inet_addr Function

Declare Function inet_addr Lib "wsock32.dll" (ByVal cp As String) As Long

Platforms

Description & Usage

inet_addr converts a string identifying an IP address into an actual IP address in network byte order. The string must be in "dotted quad" format (for example, "a.b.c.d").

Return Value

If successful, the function returns the IP address, packed into a single 32-bit integer, in network byte order. If an error occured, the function returns &HFFFFFFFF.

Visual Basic-Specific Issues

None.

Parameters

cp
The IP address string to convert. This must be in "dotted quad" format.

Example

Prompt the user for an IP address by using a regular text box. Take the IP address and determine the domain name, if any, associated with that address.

To use this example, place a command button named cmdGetDomain on a form window. Also place a text box named txtIPAddress on the 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 inet_addr Lib "wsock32.dll" (ByVal cp As String) As Long
Public Declare Function gethostbyaddr Lib "wsock32.dll" (addr As Long, ByVal length As Long, ByVal _
	protocol 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

' *** Place the following code in a form window. ***

' 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_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
	
	' Get the IP address entered by the user.
	ipAddress_n = inet_addr(txtIPAddress.Text)
	If ipAddress_n = &HFFFFFFFF Then
		Debug.Print "An incorrect 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
	
	' 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

See Also

inet_ntoa

Category

Winsock

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_addr.html