gethostbyname Function

Declare Function gethostbyname Lib "wsock32.dll" (ByVal name As String) As Long

Platforms

Description & Usage

gethostbyname gets information about a host computer. The computer is identified by its domain name. The information about the host is placed into a HOSTENT structure, a pointer to which is returned by the function. Note that gethostbyname cannot identify a host computer by its IP address string. (To get information about a computer with a particular IP address, use gethostbyaddr instead.)

Return Value

If successful, the function returns a pointer to a HOSTENT structure that contains data about the host. This pointer is only guaranteed to be valid until the next call to a Winsock function. If an error occured, the function returns zero (use WSAGetLastError to get the error code).

Visual Basic-Specific Issues

To access the returned data, use CopyMemory to copy the data referenced by the pointer into a HOSTENT structure allocated by your program. See the example for a demonstration of this.

Parameters

name
The domain name of the network host to get information about. This cannot be an IP address string.

Example

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

See Also

gethostbyaddr

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/g/gethostbyname.html