#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <WinSock2.h>
#include <iphlpapi.h>
#include <windows.h>
#include <iostream>
#pragma comment(lib,"iphlpapi")
#pragma comment(lib,"ws2_32")
using namespace std;
int main(int argc, char* argv[])
{
//init socket
WSADATA stData = { 0 };
WORD dVer = MAKEWORD(2, 2);
WSAStartup(dVer, &stData);
//net apater info
PIP_ADAPTER_INFO pAdapter = nullptr;
PIP_ADAPTER_INFO pCurrent = nullptr;
ULONG ulLen = 0L;
DWORD dwIndex = 1;
GetAdaptersInfo(pAdapter, &ulLen);
pAdapter = (PIP_ADAPTER_INFO)
VirtualAlloc(NULL, ulLen, MEM_COMMIT, PAGE_READWRITE);
if (GetAdaptersInfo(pAdapter, &ulLen) != ERROR_SUCCESS)
{
VirtualFree(pAdapter, NULL, MEM_RELEASE);
cout << "Get adapter info fail" << endl;
return -1;
}
else
{
pCurrent = pAdapter;
}
in_addr stAddr = { 0 };
while (pCurrent)
{
cout << "************************************************************" << endl;
cout << "current adapter " << dwIndex << endl;
cout << "adapter name " << pCurrent->AdapterName << endl;
cout << "adapter description " << pCurrent->Description << endl;
//使用C++的cout不懂得如何描述了直接用printf了。。。。
printf("max address %2x-%2x-%2x-%2x-%2x-%2x\n",
pCurrent->Address[0],
pCurrent->Address[1],
pCurrent->Address[2],
pCurrent->Address[3],
pCurrent->Address[4],
pCurrent->Address[5]);
//cout << "mac address "
// << hex
// << pCurrent->Address[0]
// << "-"
// << hex
// << pCurrent->Address[1]
// << "-"
// << hex
// << pCurrent->Address[2]
// << "-"
// << hex
// << pCurrent->Address[3]
// << "-"
// << hex
// << pCurrent->Address[4]
// << "-"
// << hex
// << pCurrent->Address[5]
// << endl;
cout << "adapter type " << pCurrent->Type << endl;
stAddr.S_un.S_addr = inet_addr(pCurrent->IpAddressList.IpAddress.String);
cout << "local ip " << inet_ntoa(stAddr) << endl;
stAddr.S_un.S_addr = inet_addr(pCurrent->GatewayList.IpAddress.String);
cout << "gateway ip " << inet_ntoa(stAddr) << endl;
stAddr.S_un.S_addr = inet_addr(pCurrent->IpAddressList.IpMask.String);
cout << "mask ip " << inet_ntoa(stAddr) << endl;
cout << "\n*********************************************************" << endl;
cout << endl;
dwIndex++;
pCurrent = pCurrent->Next;
}
VirtualFree(pAdapter, NULL, MEM_RELEASE);
//clear socket
system("pause");
WSACleanup();
return 0;
}