Declare Function inet_ntoa Lib "wsock32.dll" (ByVal inaddr As Long) As Long
inet_ntoa converts an IP address packed in a 32-bit integer into a human-readable string. This string is in the standard "a.b.c.d" format. This function returns a pointer to the resulting string. However, the pointer is only guaranteed to be valid until the next call to a Winsock function, so your program should make a copy of it immediately.
If successful, the function returns a pointer to the string that contains the human-readable IP address. If an error occured, the function returns zero.
To copy the returned string into a String variable, use the lstrlen and lstrcpy API functions. See the example below for a demonstration.
Print the IP address associated with a domain name specified by the user. Winsock is briefly used to resolve the domain name and format a printable IP address string. The user enters the domain name to resolve in text box txtDomain, and the domain name is resolved when the user clicks button cmdGetIP.
To run this example, place a text box named txtDomain and a command button named cmdGetIP on a form window.
' This code is licensed according to the terms and conditions listed here.
' Declarations and such needed for the example:
' (Copy them to the (declarations) section of 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
' Define a relevant API macro.
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
' *** 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
Back to the Function list.
Back to the Reference section.
Last Modified: October 29, 2000
This page is copyright © 2000 Paul Kuliniewicz.
Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/i/inet_ntoa.html