What's the best way of getting an IP address from a DNS name?
I seem to remember a call GetRemoteAddress . . . .
Printable View
What's the best way of getting an IP address from a DNS name?
I seem to remember a call GetRemoteAddress . . . .
VB Code:
' *** Place the following code inside the form window. *** Private Sub cmdGetIP_Click() Dim sockinfo As WSADATA ' information about Winsock Dim hostinfo As HOSTENT ' information about an Internet host Dim pHostinfo As Long ' pointer to a HOSTENT structure Dim pIPAddress As Long ' pointer to an IP address dword Dim ipAddress As Long ' an IP address, packed into a dword Dim pIPString As Long ' pointer to an IP address formatted as a string Dim ipString As String ' holds a human-readable IP address string Dim retval As Long ' generic return value ' Open up a Winsock session, using version 2.2. retval = WSAStartup(MAKEWORD(2, 2), sockinfo) If retval <> 0 Then Debug.Print "ERROR: Attempt to open Winsock failed: error"; retval Exit Sub End If ' Get information about the domain specified in txtDomain. pHostinfo = gethostbyname(txtDomain.Text) If pHostinfo = 0 Then Debug.Print "Unable to resolve domain name." Else ' Copy the data into a HOSTENT structure. CopyMemory hostinfo, ByVal pHostinfo, Len(hostinfo) If hostinfo.h_addrtype <> AF_INET Then Debug.Print "A non-IP address was returned." Else ' Copy the pointer to the first (and probably only) IP address in the structure. CopyMemory pIPAddress, ByVal hostinfo.h_addr_list, 4 ' Copy the actual IP address. CopyMemory ipAddress, ByVal pIPAddress, 4 ' Convert the IP address into a human-readable string. pIPString = inet_ntoa(ipAddress) ' Copy the result into a string variable. ipString = Space(lstrlen(pIPString)) retval = lstrcpy(ipString, pIPString) ' Print the result: a human-readable IP address. Debug.Print ipString End If End If ' Close the Winsock session. retval = WSACleanup() End Sub 'In a module Public Type WSADATA wVersion As Integer wHighVersion As Integer szDescription As String * 257 szSystemStatus As String * 129 iMaxSockets As Long iMaxUdpDg As Long lpVendorInfo As Long End Type Public Declare Function WSAStartup Lib "wsock32.dll" (ByVal wVersionRequested As Integer, lpWSAData _ As WSADATA) As Long Public Declare Function WSACleanup Lib "wsock32.dll" () As Long Public Type HOSTENT h_name As Long h_aliases As Long h_addrtype As Integer h_length As Integer h_addr_list As Long End Type Public Const AF_INET = 2 Public Declare Function gethostbyname Lib "wsock32.dll" (ByVal name As String) As Long Public Declare Function inet_ntoa Lib "wsock32.dll" (ByVal inaddr As Long) As Long Public Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source _ As Any, ByVal length As Long) Public Declare Function lstrlen Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As Any) As Long Public Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyA" (ByVal lpString1 As Any, ByVal _ lpString2 As Any) As Long Public Function MAKEWORD(ByVal bLow As Byte, ByVal bHigh As Byte) As Integer MAKEWORD = Val("&H" & Right("00" & Hex(bHigh), 2) & Right("00" & Hex(bLow), 2)) End Function