GetHostNameAndIP()
{
CString strIp[10];
int nCount = 0;
WORD wversionrequested;
WSADATA wsadata;
char name[255];
wversionrequested = MAKEWORD(2, 0);
if(WSAStartup(wversionrequested, &wsadata) == 0 )
{
if(gethostname(name, sizeof(name)) == 0)
{
PHOSTENT hostinfo;
hostinfo = gethostbyname(name);
for(int i = 0;hostinfo != NULL && hostinfo->h_addr_list[i] != NULL; i++)
{
strIp[i] = inet_ntoa(*(struct in_addr*)hostinfo->h_addr_list[i]); //主要在这里
nCount++;
}
}
WSACleanup();
}
}
注:以上代码需添加头文件<winsock2.h>及库文件WS2_32.LIB