Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

This function returns the standard host name for the local machine.

Syntax

int gethostname(
  char FAR* 
name,
  int 
namelen
);

Parameters

name

[out] Pointer to a buffer that receives the local host name.

namelen

[in] Length of the buffer.

Return Value

If no error occurs, this function returns zero. If an error occurs, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError.

The following table shows the possible error codes.

Error code Description

WSAEFAULT

The nameparameter is not a valid part of the user address space, or the buffer size specified by the namelenparameter is too small to hold the complete host name.

WSANOTINITIALISED

A successful WSAStartupcall must occur before using this function.

WSAENETDOWN

The network subsystem has failed.

WSAEINPROGRESS

A blocking Winsock call is in progress, or the service provider is still processing a callback function.

Remarks

This function returns the name of the local host into the buffer specified by the nameparameter. The host name is returned as a null-terminated string. The form of the host name is dependent on the Windows Sockets provider. It can be a simple host name or a fully qualified domain name. However, it is guaranteed that the name returned will be successfully parsed by gethostbynameor getaddrinfo.

Note:
If no local host name has been configured, gethostnamemust succeed and return a token host name that gethostbynameor getaddrinfocan resolve.

Requirements

Header winsock2.h
Library Ws2.lib
Windows Embedded CE Windows CE 1.0 and later
Windows Mobile Windows Mobile Version 5.0 and later

See Also