Private Type HOSTENT
hName As Long
hAliases As Long
hAddrType As Integer
hLen As Integer
hAddrList As Long
End Type
Private Declare Function gethostbyname Lib "wsock32" (ByVal hostname As String) As Long
Private Declare Sub CopyMemoryIP Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
Private Declare Function GetComputerNameA Lib "kernel32" (ByVal lpBuffer As String, nSize As Long) As Long
Public Function GetIPFromHostName(ByVal sHostName As String) As String
Dim ptrHosent As Long
Dim Host As HOSTENT
Dim dwIPAddr As Long
Dim tmpIPAddr() As Byte
Dim I As Integer
Dim sIPAddr As String
ptrHosent = gethostbyname(sHostName & vbNullChar)
If ptrHosent <> 0 Then
CopyMemoryIP Host, ptrHosent, Len(Host)
CopyMemoryIP dwIPAddr, Host.hAddrList, 4
ReDim tmpIPAddr(1 To Host.hLen)
CopyMemoryIP tmpIPAddr(1), dwIPAddr, Host.hLen
For I = 1 To Host.hLen - 1
sIPAddr = sIPAddr & tmpIPAddr(I) & "."
Next
sIPAddr = sIPAddr & tmpIPAddr(I)
GetIPFromHostName = sIPAddr
End If
End Function
Public Function GetComputerName() As String
Dim UserName As String * 255
Call GetComputerNameA(UserName, 255)
GetComputerName = Left$(UserName, InStr(UserName, Chr$(0)) - 1)
End Function
===========================
Private Sub Command1_Click()
MsgBox GetComputerName & vbCrLf & GetIPFromHostName(GetComputerName)
End Sub