' *** Place the following code in a form window. ***
Private hIPControl As Long ' handle to the IP Address control
' When the form is initialized, create an IP Address control in the
' upper-left corner of the form.
Private Sub Form_Initialize()
Dim comctls As INITCOMMONCONTROLSEX_TYPE ' identifies the control to register
Dim retval As Long ' generic return value
' Register the IP Address control window class.
With comctls
.dwSize = Len(comctls)
.dwICC = ICC_INTERNET_CLASSES
End With
retval = InitCommonControlsEx(comctls)
' Create the IP Address control in the corner of the window.
hIPControl = CreateWindowEx(0, WC_IPADDRESS, "", WS_CHILD Or WS_VISIBLE, 0, 0, 125, 20, _
Me.hWnd, 0, App.hInstance, ByVal CLng(0))
End Sub
' Destroy the IP Address control when the form closes.
Private Sub Form_Unload(Cancel As Integer)
Dim retval As Long ' return value
retval = DestroyWindow(hIPControl)
End Sub
' Look up the primary domain name of the host computer identified by the
' address in the IP Address control.
Private Sub cmdGetDomain_Click()
Dim ipAddress_h As Long ' the IP address, in host byte order
Dim ipAddress_n As Long ' the IP address, in network byte order
Dim sockinfo As WSADATA ' information about the Winsock implementation
Dim pHostinfo As Long ' pointer to information about the host computer
Dim hostinfo As HOSTENT ' information about the host computer
Dim domainName As String ' the primary domain name of the host computer
Dim retval As Long ' generic return value
' Verify that an IP address was entered.
retval = SendMessage(hIPControl, IPM_ISBLANK, ByVal CLng(0), ByVal CLng(0))
If retval <> 0 Then
Debug.Print "No IP address was entered!"
Exit Sub
End If
' Get the IP address entered by the user and verify that all
' four fields in the address were entered.
retval = SendMessage(hIPControl, IPM_GETADDRESS, ByVal CLng(0), ipAddress_h)
If retval < 4 Then
Debug.Print "An incomplete IP address was entered!"
Exit Sub
End If
' Open up a Winsock v2.2 session.
retval = WSAStartup(&H202, sockinfo)
If retval <> 0 Then
Debug.Print "ERROR: Attempt to open Winsock failed: error"; retval
Exit Sub
End If
' Convert the IP address into network byte order.
ipAddress_n = htonl(ipAddress_h)
' Get information about the host computer.
pHostinfo = gethostbyaddr(ipAddress_n, 4, AF_INET)
If pHostinfo = 0 Then
Debug.Print "Could not find a host with the specified IP address."
Else
' Copy the data into the structure.
CopyMemory hostinfo, ByVal pHostinfo, Len(hostinfo)
' Copy the host domain name into a string.
domainName = Space(lstrlen(hostinfo.h_name))
retval = lstrcpy(domainName, hostinfo.h_name)
Debug.Print "Domain name is: "; domainName
End If
' End the Winsock session.
retval = WSACleanup()
End Sub