由于一个需求需要获取网卡的MAC地址,就搜了一下,大部分都是COPY来COPY去的一些代码,有很多甚至不能直接运行或有还有内存泄漏。自己查了一下MSDN然后封装了一下;
需要注意,一个机器可能有多个网卡(包含虚拟网卡),一个网卡可能有多个IP
主要用到了 GetAdaptersInfo 这个API,可以自己去MSDN查一下
下面是我的封装,可以直接用,也可以按照你的需求改,我这里只需要获取MAC地址,IP,和描述就够了,通过描述可以筛选掉VMware和VPN生成的虚拟网卡
// 头文件包含 #include "stdafx.h" #include <WinSock2.h> #include <Iphlpapi.h> #include <iostream> #include <string> #include <map> #include <vector> #pragma comment(lib,"iphlpapi.lib") #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) #define FREE(x) HeapFree(GetProcessHeap(), 0, (x)) typedef struct _MyAdpterInfo { std::vector<std::string> Ip; std::string MacAddress; std::string Description; std::string Name; UINT Type; }MyAdpterInfo; int MyGetAdptersInfo(std::vector<MyAdpterInfo>& adpterInfo) { PIP_ADAPTER_INFO pAdapterInfo; PIP_ADAPTER_INFO pAdapter = NULL; DWORD dwRetVal = 0; UINT i; /* variables used to print DHCP time info */ struct tm newtime; char buffer[32]; errno_t error; ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO); pAdapterInfo = (IP_ADAPTER_INFO *)MALLOC(sizeof(IP_ADAPTER_INFO)); if (pAdapterInfo == NULL) { printf("Error allocating memory needed to call GetAdaptersinfo\n"); return -1; } // Make an initial call to GetAdaptersInfo to get // the necessary size into the ulOutBufLen variable if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) { FREE(pAdapterInfo); pAdapterInfo = (IP_ADAPTER_INFO *)MALLOC(ulOutBufLen); if (pAdapterInfo == NULL) { printf("Error allocating memory needed to call GetAdaptersinfo\n"); return -1; // error data return } } if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) { pAdapter = pAdapterInfo; while (pAdapter) { MyAdpterInfo info; info.Name = std::string(pAdapter->AdapterName); info.Description = std::string(pAdapter->Description); info.Type = pAdapter->Type; char buffer[20]; sprintf_s(buffer, "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x", pAdapter->Address[0], pAdapter->Address[1], pAdapter->Address[2], pAdapter->Address[3], pAdapter->Address[4], pAdapter->Address[5]); info.MacAddress = std::string(buffer); IP_ADDR_STRING *pIpAddrString = &(pAdapter->IpAddressList); do { info.Ip.push_back(std::string(pIpAddrString->IpAddress.String)); pIpAddrString = pIpAddrString->Next; } while (pIpAddrString); pAdapter = pAdapter->Next; adpterInfo.push_back(info); } if (pAdapterInfo) FREE(pAdapterInfo); return 0; // normal return } else { if (pAdapterInfo) FREE(pAdapterInfo); printf("GetAdaptersInfo failed with error: %d\n", dwRetVal); return 1; // null data return } } int _tmain(int argc, _TCHAR* argv[]) { std::vector<MyAdpterInfo> AdptersInfo; int ret = MyGetAdptersInfo(AdptersInfo); for (int i = 0; i < AdptersInfo.size(); ++i) { std::cout << "Adpter name: " << AdptersInfo[i].Name << std::endl; std::cout << "Adpter description: " << AdptersInfo[i].Description << std::endl; std::cout << "Adpter MAC: " << AdptersInfo[i].MacAddress << std::endl; std::cout << "Adpter IP: "; for (int j = 0; j < AdptersInfo[i].Ip.size(); ++j) { if (j != 0) { std::cout << ", "; } std::cout << AdptersInfo[i].Ip[j]; } std::cout << std::endl << std::endl << std::endl; } return 0; }
运行结果