#include <KHAboutString.h>
//获取网关mac
std_wstring get_gate_mac()
{
std::map< std_wstring, std::vector< std_wstring > > ip_config;
KHAboutLocalEnvirTool::LocalIpconfigListEx(ip_config);
std_wstring gateways,gate_mac;
for ( auto it = ip_config.begin(); it != ip_config.end(); it++ )
{
if ( it->second.size() >= 2 )
{
if ( it->second[2] != L"" )
{
gateways = it->second[2];
}
}
}
KHAboutLocalEnvirTool::GetRemoteMacByIP( gateways.c_str(), gate_mac );
return gate_mac;
}
//获取本地mac
std_wstring get_local_mac()
{
std::map< std_wstring, std::vector< std_wstring > > ip_config;
KHAboutLocalEnvirTool::LocalIpconfigListEx(ip_config);
std_wstring local_mac;
for ( auto it = ip_config.begin(); it != ip_config.end(); it++ )
{
if ( it->second.size() >= 2 )
{
if ( it->second[2] != L"" )
{
local_mac = it->second[0];
break;
}
}
}
return local_mac;
}
//如果是无线网卡,获取网关mac
#include "NtDDNdis.h"
#include "RegByWan.h"
long GetNICs(std::vector<std::pair<std_string, std_string>>& nics)
{
std::string cstrFindSubkey="";
HKEY hKey=NULL,hSubkey=NULL;
CHAR cstrNic_desc[MAX_PATH];
CHAR cstrNic_guid[MAX_PATH];
CHAR cstrName[MAX_PATH];
DWORD dwSubkeyCount=0;
DWORD dwNameLen=0;
DWORD dwFindValueLen=0;
DWORD dwFindValueType=0;
long ret=0;
nics.clear();
ret=RegOpenKeyExA(HKEY_LOCAL_MACHINE,NETWORD_CARDS, 0, KEY_READ, &hKey);
if (ret != ERROR_SUCCESS)
return ret;
ret=RegQueryInfoKey(hKey,NULL,NULL,NULL,&dwSubkeyCount,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
if(ret!=ERROR_SUCCESS)
return ret;
if(dwSubkeyCount>0)
{
for(DWORD i=0;i<dwSubkeyCount;i++)
{
dwNameLen=MAX_PATH;
if(RegEnumKeyExA(hKey,i,cstrName,&dwNameLen,NULL,NULL,NULL,NULL)==ERROR_SUCCESS)
{
cstrFindSubkey=NETWORD_CARDS;
cstrFindSubkey+="\\";
cstrFindSubkey+=cstrName;
if(RegOpenKeyExA(HKEY_LOCAL_MACHINE, cstrFindSubkey.c_str(), 0, KEY_READ, &hSubkey)==ERROR_SUCCESS)
{
ZeroMemory(cstrNic_desc,MAX_PATH);
ZeroMemory(cstrNic_guid,MAX_PATH);
dwFindValueLen=MAX_PATH;
RegQueryValueExA(hSubkey,"Description",NULL,&dwFindValueType,(LPBYTE)cstrNic_desc,&dwFindValueLen);
dwFindValueLen=MAX_PATH;
RegQueryValueExA(hSubkey,"ServiceName",NULL,&dwFindValueType,(LPBYTE)cstrNic_guid,&dwFindValueLen);
nics.push_back(std::make_pair(cstrNic_desc, cstrNic_guid));
}
}
}
}
return ret;
}
long GetPermanentAddressFromNIC(std::string nic_guid,std::string& mac)
{
NDIS_OID ndisOid = OID_802_3_PERMANENT_ADDRESS;
std::string nic_if("\\\\.\\" + nic_guid);
HANDLE hNic = CreateFileA(nic_if.c_str(), GENERIC_READ,
0, NULL, OPEN_EXISTING, 0, NULL);
if (hNic == INVALID_HANDLE_VALUE) {
return GetLastError();
}
UCHAR Mac[6];
CHAR buf[4]={0};
DWORD rBytes = 0;
if (!DeviceIoControl(hNic, IOCTL_NDIS_QUERY_GLOBAL_STATS,
&ndisOid, sizeof(NDIS_OID), Mac, sizeof(Mac), &rBytes, NULL))
{
DWORD err = GetLastError();
CloseHandle(hNic);
return err;
}
mac.clear();
for(size_t i=0;i<6;i++)
{
sprintf_s(buf,"%0.2X",Mac[i]);
mac+=buf;
mac+="-";
}
mac.pop_back();
CloseHandle(hNic);
return 0;
}
bool GetMacAddress(std::wstring &wstrMacAddress)
{
std::vector<std::pair<std_string, std_string> > nics;
WCHAR *wstrBuf=NULL;
LONG ec=GetNICs(nics);
if(ec!=0)
return false;
wstrMacAddress.clear();
for(size_t i = 0; i < nics.size(); i++)
{
std::string cstrmac;
std::wstring wstrmac;
GetPermanentAddressFromNIC(nics[i].second, cstrmac);
if ( cstrmac.size()>0 )
{
USES_CONVERSION;
wstrmac = A2W(cstrmac.c_str());
wstrMacAddress+=wstrmac;
wstrMacAddress+=L",";
}
}
if(wstrMacAddress.size()>0)
wstrMacAddress.pop_back();
return true;
}
std_wstring get_local_mac()
{
std_wstring local_mac;
GetMacAddress(local_mac);
return local_mac;
}
备注:有时候可能遇到权限不够
KHAboutPrivilege::EnableDebugPrivilege(true);
KHAboutPrivilege::EnableLoadDriverPrivileges();
KHAboutPrivilege::EnableShutdownPrivileges();
KHAboutPrivilege::EnableTermProcessPrivileges();