Windows系统下获取本机IP
#pragma comment(lib, "ws2_32.lib")WSADATA wsd;
WSAStartup(MAKEWORD(2, 2), &wsd);
DWORD size = MAX_PATH;
char computerName = { 0 };
GetComputerNameA(computerName, &size);
CString str;
struct hostent* pHost = nullptr;
pHost = gethostbyname(computerName);
for (int i = 0; i < 4; i++)
{
CString addr;
if (i > 0)
{
str += _T(".");
}
addr.Format(_T("%u"), (UINT)((UCHAR*)(pHost->h_addr_list)));
str += addr;
}
SetWindowText(str);
WSACleanup();
页:
[1]