BOOL GetIPAddressList(CStringArray *pArray)
{
pArray->RemoveAll();
CString strBuffer;
WORD wVersionRequested;
WSADATA wsaData;
char charHostName[255];
HOSTENT *infoHost = NULL;
wVersionRequested = MAKEWORD(2, 2);
if (WSAStartup(wVersionRequested, &wsaData) != 0)
{
throw _T("WSAStartup()!");
}
if (gethostname(charHostName, sizeof(charHostName)) != 0)
{
throw _T("gethostname()!");
}
if ((infoHost = gethostbyname(charHostName)) == NULL)
{
throw _T("gethostbyname()!");
}
for (INT i = 0; ; i++)
{
CString strBuffer = inet_ntoa( *(IN_ADDR*)infoHost->h_addr_list[i]) ;
pArray->Add(strBuffer);
if (infoHost->h_addr_list[i] + infoHost->h_length >= infoHost->h_name)
break;
}
WSACleanup();
return TRUE;
}
C++ 获取本机IP
最新推荐文章于 2023-11-08 17:49:42 发布