WSADATA wsadata;
if(0 != WSAStartup(MAKEWORD(2, 2), &wsadata))
{
AfxMessageBox("初始化网络环境失败!");
return TRUE;
}
char szHostName[MAX_PATH + 1];
gethostname(szHostName, MAX_PATH);
hostent *p = gethostbyname(szHostName);
if(p == NULL)
{
AfxMessageBox("得到本机网络信息失败!");
return TRUE;
}
m_strName = szHostName;
int nCount = 0;
char *pIP = inet_ntoa(*(in_addr *)p->h_addr_list[0]);
m_strAddr = pIP;
#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#pragma comment(lib,"Iphlpapi.lib")
int main()
{
// 初始化winsock
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
pAdapterInfo = (PIP_ADAPTER_INFO)malloc(ulOutBufLen);
DWORD dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen);
// 第一次调用GetAdapterInfo获取ulOutBufLen大小
if (dwRetVal == ERROR_BUFFER_OVERFLOW)
{
free(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen);
}
if (dwRetVal == NO_ERROR)
{
pAdapter = pAdapterInfo;
while (pAdapter)
{
printf("Adapter Name: %s
", pAdapter->AdapterName);
printf("Adapter Desc: %s
", pAdapter->Description);
printf("MAC Addr: %02x-%02x-%02x-%02x-%02x-%02x
",
pAdapter->Address[0],
pAdapter->Address[1],
pAdapter->Address[2],
pAdapter->Address[3],
pAdapter->Address[4],
pAdapter->Address[5]);
printf("IP Address: %s
", pAdapter->IpAddressList.IpAddress.String);
printf("IP Mask: %s
", pAdapter->IpAddressList.IpMask.String);
printf("Gateway: %s
", pAdapter->GatewayList.IpAddress.String);
pAdapter = pAdapter->Next;
}// end while
}
else
{
printf("Call to GetAdaptersInfo failed.
");
}
...卸载winsock ...
return 0;
} //end main