WORD wVersion;
WSADATA wsaData;
char name[255];
CString ip;
PHOSTENT host;
wVersion = MAKEWORD (2, 0);
if( WSAStartup (wVersion, &wsaData) == 0 ) //打开winsock库
{
if( gethostname (name, sizeof(name)) == 0 ) // 获得主机名
{
if((host = gethostbyname (name) ) != NULL ) //获得主机信息
{
ip = inet_ntoa (*(in_addr*)*host->h_addr_list); //获得主机IP
MessageBox(ip);
}
}
WSACleanup(); //关闭winsock库
}
需要 winsock2.h 和 WS2_32.lib