Declare Function gethostname Lib "ws2_32.dll" (ByVal host_name As String, _
ByVal namelen As Long) As Long
Private Sub cmdGet_Click()
'----------------------------------------------------
'pointer to HOSTENT structure returned by
'the gethostbyname function
Dim lngPtrToHOSTENT As Long
'
'structure which stores all the host info
Dim udtHostent As HOSTENT
'
'pointer to the IP address' list
Dim lngPtrToIP As Long
'
'byte array that contains elemets of an IP address
Dim arrIpAddress() As Byte
'
'result IP address string to add into the ListBox
Dim strIpAddress As String
'
'buffer string to receive the local system host name
Dim strHostName As String * 256
'
'value returned by the gethostname function
Dim lngRetVal As Long
'----------------------------------------------------
'
'Clear the ListBox control
List1.Clear
'
'Get the local host name
lngRetVal = gethostname(strHostName, 256)
'
If lngRetVal = SOCKET_ERROR Then
ShowErrorMsg (Err.LastDllError)
Exit Sub
End If
'
Text1.Text = Left(strHostName, InStr(1, strHostName, Chr(0)) - 1)
'
'Call the gethostbyname Winsock API function
'to get pointer to the HOSTENT structure
lngPtrToHOSTENT = GetHostByName(Trim$(Text1.Text))
'
'Check the lngPtrToHOSTENT value
If lngPtrToHOSTENT = 0 Then
'
'If the gethostbyname function has returned 0
'the function execution is failed. To get
'error description call the ShowErrorMsg
'subroutine
'
ShowErrorMsg (Err.LastDllError)
'
Else
'
'The gethostbyname function has found the address
'
'Copy retrieved data to udtHostent structure
RtlMoveMemory udtHostent, lngPtrToHOSTENT, LenB(udtHostent)
'
'Now udtHostent.hAddrList member contains
'an array of IP addresses
'
'Get a pointer to the first address
RtlMoveMemory lngPtrToIP, udtHostent.hAddrList, 4
'
Do Until lngPtrToIP = 0
'
'Prepare the array to receive IP address values
ReDim arrIpAddress(1 To udtHostent.hLength)
'
'move IP address values to the array
RtlMoveMemory arrIpAddress(1), lngPtrToIP, udtHostent.hLength
'
'build string with IP address
For i = 1 To udtHostent.hLength
strIpAddress = strIpAddress & arrIpAddress(i) & "."
Next
'
'remove the last dot symbol
strIpAddress = Left$(strIpAddress, Len(strIpAddress) - 1)
'
'Add IP address to the listbox
List1.AddItem strIpAddress
'
'Clear the buffer
strIpAddress = ""
'
'Get pointer to the next address
udtHostent.hAddrList = udtHostent.hAddrList + LenB(udtHostent.hAddrList)
RtlMoveMemory lngPtrToIP, udtHostent.hAddrList, 4
'
Loop
'
End If
'
End Sub
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum