The gethostbyname function retrieves host information corresponding to a host name from a host database.
void gethostbyname( a );
The gethostbyname function returns a pointer to a hostent structure—a structure allocated by Windows Sockets. The hostent structure contains the results of a successful search for the host specified in the name parameter.
If the host specified in the name parameter has both IPv4 and IPv6 addresses, only the IPv4 addresses will be returned. The gethostbyname function can only return IPv4 addresses for the name parameter. The getaddrinfo function and associated addrinfo structure should be used if IPv6 addresses for the host are required or if both IPv4 and IPv6 addresses for the host are required.
If the name parameter points to an empty string or name is NULL, the returned string is the same as the string returned by a successful gethostname function call (the standard host name for the local computer).
If the name parameter contains a string representation of a legal IPv4 address, then the binary IPv4 address that represents the string is returned in the hostent structure. The h_name member of the hostent structure contains the string representation of the IPv4 address and the h_addr_list contains the binary IPv4 address. If the name parameter contains a string representation of an IPv6 address or an illegal IPv4 address, then the gethostbyname function will fail and return WSANO_DATA.
The memory for the hostent structure returned by the gethostbyname function is allocated internally by the Winsock DLL from thread local storage. Only a single hostent structure is allocated and used, no matter how many times the gethostbyaddr or gethostbyname functions are called on the thread. The returned hostent structure must be copied to an application buffer if additional calls are to be made to the gethostbyname or gethostbyaddr functions on the same thread. Otherwise, the return value will be overwritten by subsequent gethostbyname or gethostbyaddr calls on the same thread. The internal memory allocated for the returned hostent structure is released by the Winsock DLL when the thread exits.
An application should not try to release the memory used by the returned hostent structure. The application must never attempt to modify this structure or to free any of its components. Furthermore, only one copy of this structure is allocated per thread, so the application should copy any information it needs before issuing any other function calls to gethostbyname or gethostbyaddr .
The gethostbyname function cannot take an IP address string as a parameter passed to it in the name and resolve it to a host name. Such a request is treated exactly as a string representation of an IPv4 address or an unknown host name were passed. An application can use the inet_addr to convert an IPv4 address string to a binary IPv4 address, then use another function, gethostbyaddr, to resolve the IPv4 address to a host name.
#include <winsock2.h> #include <ws2tcpip.h> #include <stdio.h> #include <windows.h> #pragma comment(lib, "ws2_32.lib")
Windows 8.1 and Windows Server 2012 R2: This function is supported for Windows Store apps on Windows 8.1, Windows Server 2012 R2, and later.
|Windows version||Windows 8.1, Windows Vista [desktop apps||UWP apps] Windows Server 2003 [desktop apps||UWP apps]|
|Header||wsipv6ok.h (include Winsock2.h, Winsock.h)|