gethostname Function

Declare Function gethostname Lib "wsock32.dll" (ByVal name As String, ByVal namelen As Long) As Long

Platforms

Description & Usage

gethostname retrieves the network host name for the computer. This could be a simple host name or a fully-qualified domain name. No matter what it is, though, it can be successfully given to gethostbyname to get information about the computer as a network host (for example, to get its IP address).

Return Value

If successful, the function returns zero. If an error occured, the function returns a non-zero value (use WSAGetLastError to get the error code).

Visual Basic-Specific Issues

None.

Parameters

name
String that receives the host name of the computer, terminated by a null character. This string must have sufficient room to receive the string copied into it.
namelen
The length of the string passed as name.

Example

When the form window opens, create an IP address control in the upper-left corner and initialize it to the IP address of the computer. No special effort is needed to run this example, since the IP Address control is created programmatically when the form loads.

' 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 gethostname Lib "wsock32.dll" (ByVal name As String, ByVal namelen As Long) As Long
Public Declare Function gethostbyname Lib "wsock32.dll" (ByVal name As String) As Long
Public Declare Function ntohl Lib "wsock32.dll" (ByVal hostlong 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 Type INITCOMMONCONTROLSEX_TYPE
	dwSize As Long
	dwICC As Long
End Type
Public Declare Function InitCommonControlsEx Lib "comctl32.dll" (lpInitCtrls As _
	INITCOMMONCONTROLSEX_TYPE) As Long
Public Const ICC_INTERNET_CLASSES = &H800
Public Declare Function CreateWindowEx Lib "user32.dll" Alias "CreateWindowExA" (ByVal dwExStyle As Long, _
	ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x _
	As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, _
	ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
Public Const WC_IPADDRESS = "SysIPAddress32"
Public Const WS_CHILD = &H40000000
Public Const WS_VISIBLE = &H10000000
Public Declare Function DestroyWindow Lib "user32.dll" (ByVal hWnd As Long) As Long
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal Msg _
	As Long, wParam As Any, lParam As Any) As Long
Public Const IPM_SETADDRESS = &H465

' 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 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 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 localhostName As String  ' the computer's domain name
	Dim pIPAddress As Long   ' pointer to an IP address dword
	Dim ipAddress_n As Long    ' the IP address in network byte order
	Dim ipAddress_h As Long  ' the IP address in host byte order
	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))
	
	' Open a new Winsock session (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 the domain name of the computer, or, failing that, a string
	' that gethostbyname can handle to give an IP address.
	localhostName = Space(256)
	retval = gethostname(localhostName, 256)
	localhostName = Left(localhostName, InStr(localhostName, vbNullChar) - 1)
	' Get information about this computer on the network.
	' Get information about the domain specified in txtDomain.
	pHostinfo = gethostbyname(localhostName)
	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_n, ByVal pIPAddress, 4
			' Convert it to host byte order.
			ipAddress_h = ntohl(ipAddress_n)
			' Set the IP Address control to hold this address.
			retval = SendMessage(hIPControl, IPM_SETADDRESS, _
				ByVal CLng(0), ByVal ipAddress_h)
		End If
	End If
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

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/gethostname.html