VC uses gethostbyname to resolve the Domain Name
The source code is later. The source code uses MFC and you need to import the # include "winsock2.h" header file. You need to import ws2_32.lib to modify this Code and make it into other applications.
The source code is as follows:
Int status;
Wsadata;
Char sztemp1 [80];
Struct in_addr ADDR;
If (status = wsastartup (makeword (1, 1), & wsadata) = 0)
{
MessageBox (wsadata. szdescription, wsadata. szsystemstatus, mb_ OK );
}
Else {
Sprintf (sztemp1, "% d is the Err", status );
MessageBox (sztemp1, "error", mb_ OK );
}
Phostent Phe;
PHE = gethostbyname ("www.g.cn ");
If (PHE = NULL ){
Char sztemp [200];
Sprintf (sztemp, "% d is the error. Make sure '% s' is listed in the hosts file.", wsagetlasterror (), "www.g.cn ");
MessageBox (sztemp, "gethostbyname () failed.", mb_ OK );
Return;
}
Else
{
ADDR. s_addr = * (u_long *) phe-> h_addr_list [0];
MessageBox (inet_ntoa (ADDR ));
}
After execution, a dialog box is displayed, showing the address of the current domain name: