In the previous tutorial we have found out how to get an IP address of the remote host by its name. We have used the gethostbyname function to achieve that and we can use that function in order to get an IP address of the local system as well. But how to get a host name of the local system? The Winsock API provides us with the function which just does what we need - gethostname. This function retrieves the local host name and we can use that name in order to get all the IP addresses of the local system.
Declare Function gethostname Lib "ws2_32.dll" (ByVal host_name As String, _
ByVal namelen As Long) As Long
The function receives two arguments. The first argument is just a string buffer in which the function puts retrieved host name, and the second one is a length of that buffer. The typical usage of the function is shown below:
...
Dim strHostName As String * 256 'buffer
Dim lngRetVal As Long 'returned value
...
lngRetVal = gethostname(strHostName, 256)
...
If no error occurs, the function returns zero. Otherwise, it returns SOCKET_ERROR and a specific error code can be retrieved with Err.LastDllError.
...
Dim strHostName As String * 256 'buffer
Dim lngRetVal As Long 'returned value
...
lngRetVal = gethostname(strHostName, 256)
If lngRetVal = SOCKET_ERROR Then
'
ShowErrorMsg (Err.LastDllError)
'
Else
'
Debug.Print "Local host name: " & strHostName
'
End If
...
Possible error codes are in the table:
Error code Meaning
WSAEFAULT The name parameter is not a valid part of the user address space, or the buffer size specified by namelen parameter is too small to hold the complete host name.
WSANOTINITIALISED A successful WSAStartup call must occur before using this function.
WSAENETDOWN The network subsystem has failed.
WSAEINPROGRESS A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.
Creating sample application
We don't need much to create the sample. Open the project that we worked on in the previous tutorial, and just add the highlighted code into the cmdGet_Click event procedure:
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
That's it. In the next part we'll learn how to get a host name by its IP address.
--------------------------------------------------------------------------------