Windows Mobile 6.5
A version of this page is also available for

This function retrieves host information corresponding to a host name from a host database.

The gethostbyname function has been deprecated by the introduction of the getaddrinfo function. Developers who create Windows Sockets 2 applications are urged to use the getaddrinfo function instead of gethostbyname.

struct hostent FAR* gethostbyname(
  const char FAR* name 


[out] Pointer to the null-terminated name of the host to resolve.

If no error occurs, this function returns a pointer to the hostent structure. If an error occurs, it returns a NULL pointer, and a specific error number can be retrieved by calling WSAGetLastError.

The following table shows the possible error codes.

Error code Description


A successful WSAStartup call must occur before using this function.


The network subsystem has failed.


An authoritative answer host was not found.


A nonauthoritative host was not found, or the server failure.


A nonrecoverable error occurred.


A valid name exists, but no data record of the requested type exists.


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


The name parameter is not a valid part of the user address space.


The socket was closed.

Japanese Version Winsock calls to get the machine IP address will fail if the machine name is entered in Japanese characters.

This function returns a pointer to a hostent structure, which is a structure allocated by Windows Sockets. The hostent structure contains the results of a successful search for the host specified in the name parameter.

The application must never attempt to modify this structure or to free anything inside the structure.. Furthermore, only one copy of this structure is allocated per thread, so the application should copy any information it needs before issuing any other Windows Sockets function calls.

This function cannot resolve IP address strings passed to it. Such a request is treated exactly as if an unknown host name were passed. Use inet_addr to convert an IP address string to an actual IP address, then use another function, gethostbyaddr, to obtain the contents of the hostent structure.

The gethostbyname function resolves the string returned by a successful call to gethostname.

Windows Embedded CEWindows CE 1.0 and later
Windows MobileWindows Mobile Version 5.0 and later