The gethostname function

Description

The gethostname function retrieves the host name of the local computer. This host name can be a simple host name or a fully qualified domain name, but whatever its form the host name retrieved is guaranteed to be successfully parsed by the gethostbyname function. This is true even if no host name has been configured for the local computer.

Declaration

The system include file WinSock2.inc contains the following declaration for the gethostname function:

function gethostname(name : address; namelen : integer) : integer;
  external dll='ws2_32.dll';

Arguments

The First Argument

The first argument passed to the gethostname function is the operating system address of the buffer that you supply to store the host name retrieved. The type of this argument is the built-in type address, so you must use the built-in function addr to get the operating system address of the buffer. The host name that will be put into this buffer by the gethostname function is a null-terminated string (i.e. a cstring).

The Second Argument

The second argument passed to the gethostname function, is the length of the buffer you supplied to store the host name.

Return Values

The gethostname function returns a value of integer type that indicates whether the call was successful. A value of zero means that the call succeeded. A value of SOCKET_ERROR indicates that the called failed, and in this case you can use the WSAGetLastError function to retrieve a code that identifies the error that caused the call to fail. NOTE: The constant SOCKET_ERROR is declared in the system include file WinSock2.inc.

Reference Information

The authoritative source of information about the WinSock2 library is the Microsoft Developers Network (MSDN). You can access the MSDN on the Microsoft website at msdn.microsoft.com.