#include <iostream>
#include <strsafe.h>
#include <WinSock2.h>
#include <Iphlpapi.h>
#include <vector>
#pragma comment(lib,"Iphlpapi.lib")
using namespace std;
bool GetRegInfo(HKEY hRoot, LPCSTR szSubKey, LPCSTR szValueName, LPSTR szRegInfo)
{
HKEY hKey;
DWORD dwType = REG_SZ;
DWORD dwLenData = strlen(szRegInfo);
LONG lRes = RegCreateKeyExA(hRoot, szSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL);
if (lRes != ERROR_SUCCESS)
{
if (lRes == 5)
{
printf("Please use Administrator Privilege !\n");
}
else
{
printf("Get Register Info Error! Error Code is ");
printf("%ld\n", lRes);
}
RegCloseKey(hKey);
RegCloseKey(hRoot);
return false;
}
RegQueryValueExA(hKey, szValueName, 0, &dwType, NULL, &dwLenData);
lRes = RegQueryValueExA(hKey, szValueName, 0, &dwType, (LPBYTE)szRegInfo, &dwLenData);
if (lRes != ERROR_SUCCESS)
{
RegCloseKey(hKey);
RegCloseKey(hRoot);
return false;
}
RegCloseKey(hKey);
RegCloseKey(hRoot);
return true;
}
int IsPCINetCard(const PIP_ADAPTER_INFO pIpAdapterInfo)
{
//通过注册表特征去除非物理网卡
char szRegSubKey[255] = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\";
char szNetCardRegInfo[255] = "\0";
StringCchCatA(szRegSubKey, sizeof(szRegSubKey), pIpAdapterInfo->AdapterName);
StringCchCatA(szRegSubKey, sizeof(szRegSubKey), "\\Connection");
if (!GetRegInfo(HKEY_LOCAL_MACHINE, szRegSubKey, "PnPInstanceId", szNetCardRegInfo))
{
return -2;
}
if (strncmp(szNetCardRegInfo, "PCI", 3) == 0) return 0;
else return -1;
}
void OutputMACs(PIP_ADAPTER_INFO pIpAdapterInfo, vector<string>& vstrMAC)
{
//可能有多网卡,因此通过循环去判断
while (pIpAdapterInfo)
{
if (pIpAdapterInfo->Type != MIB_IF_TYPE_ETHERNET)
{
pIpAdapterInfo = pIpAdapterInfo->Next;
continue;
}
//去除非PCI物理网卡
if (IsPCINetCard(pIpAdapterInfo) != 0)
{
if (IsPCINetCard(pIpAdapterInfo) == -2)
{
return;
}
pIpAdapterInfo = pIpAdapterInfo->Next;
continue;
}
//输出信息
char chMAC[32];
sprintf_s(chMAC, 32, "%02X%02X%02X%02X%02X%02X",
int(pIpAdapterInfo->Address[0]),
int(pIpAdapterInfo->Address[1]),
int(pIpAdapterInfo->Address[2]),
int(pIpAdapterInfo->Address[3]),
int(pIpAdapterInfo->Address[4]),
int(pIpAdapterInfo->Address[5]));
vstrMAC.push_back(chMAC);
pIpAdapterInfo = pIpAdapterInfo->Next;
}
return;
}
int main(void)
{
string mac = "000000000000";
PIP_ADAPTER_INFO pIpAdapterInfo = (PIP_ADAPTER_INFO)malloc(sizeof(IP_ADAPTER_INFO));
unsigned long stSize = sizeof(IP_ADAPTER_INFO);
int nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize);
if (ERROR_BUFFER_OVERFLOW == nRel/*GetAdaptersInfo参数传递的内存空间不足*/)
{
//free(pIpAdapterInfo);
pIpAdapterInfo = (PIP_ADAPTER_INFO)realloc(pIpAdapterInfo, stSize);
nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize);
}
if (ERROR_SUCCESS == nRel)
{
vector<string> vstrMAC;
OutputMACs(pIpAdapterInfo, vstrMAC);
cout << "MAC number:" << vstrMAC.size() << endl;
for (auto iter = vstrMAC.begin(); iter != vstrMAC.end(); iter++)
{
cout << "MAC addr:" << *iter << endl;
}
mac = *(vstrMAC.begin());
for (auto ite = vstrMAC.begin(); ite != vstrMAC.end(); ++ite)
{
if (mac.compare(*ite) < 0)
{
mac = *ite;
}
}
cout << "max: " << mac << endl;
}
if (pIpAdapterInfo)
{
free(pIpAdapterInfo);
}
system("pause");
return 0;
}
获取本地物理MAC地址(方法一)
最新推荐文章于 2022-03-23 16:39:27 发布