|
C++ 获取物理Mac地址方法:
1. 使用GetAdaptersInfo获取网卡详细信息;
2. 遍历IP_ADAPTER_INFO,取AdapterName去匹配注册表HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}\\下子节点的NetCfgInstanceId值,找到相对应的子节点再取Characteristics值;
3. 判断Characteristics值 & NCF_PHYSICAL(4)的结果,大于0为物理网卡;
//32位程序在64位系统下运行禁用重定向问题;
CWow64Detector::CWow64Detector(BOOL bDisable)
: m_bDisable(FALSE)
{
if (bDisable && m_bWin64)
m_bDisable = Win64DisableFsRedirection(&m_pDisData);
}
CWow64Detector::~CWow64Detector()
{
if (m_bDisable)
{
Win64RevertFsRedirection(m_pDisData);
}
}
BOOL IsPhysicalMac(char* pAdapterName)
{
CWow64Detector wow64(TRUE); //该类封装了32位程序在64位系统中的重定向问题;否则注册表查询会有问题;
CString strAdapterName(pAdapterName);
LPCTSTR lpszKey = _T("SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}");
HKEY hSubKey = NULL;
if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpszKey, 0, KEY_QUERY_VALUE| KEY_ENUMERATE_SUB_KEYS, &hSubKey) != ERROR_SUCCESS) {
return FALSE;
}
BOOL nRet = FALSE, bFind = FALSE;
TCHAR achClass[MAX_PATH] = _T("");
DWORD cchClassName = MAX_PATH;
DWORD cSubKeys = 0;
DWORD cbMaxSubKey;
DWORD cchMaxClass;
DWORD cValues;
DWORD cchMaxValue;
DWORD cbMaxValueData;
DWORD cbSecurityDescriptor;
FILETIME ftLastWriteTime;
if (::RegQueryInfoKey(hSubKey, achClass, &cchClassName, NULL,
&cSubKeys, &cbMaxSubKey,&cchMaxClass, &cValues, &cchMaxValue,
&cbMaxValueData, &cbSecurityDescriptor, &ftLastWriteTime) == ERROR_SUCCESS) {
for (size_t i = 0; i < cSubKeys; i++)
{
TCHAR achKey[MAX_PATH] = _T("");
DWORD achName = MAX_PATH;
LSTATUS lp = ::RegEnumKeyEx(hSubKey, i, achKey, &achName,
NULL, NULL, NULL, &ftLastWriteTime);
if (lp == ERROR_SUCCESS) {
HKEY hachKey = NULL;
if (::RegOpenKeyEx(hSubKey, achKey, 0, KEY_QUERY_VALUE, &hachKey) == ERROR_SUCCESS) {
DWORD dwType = 0, dwSize = 0, dwValue = 0;
::RegQueryValueEx(hachKey, _T("NetCfgInstanceId"), NULL, &dwType, NULL, &dwSize);
if (dwType == REG_SZ && dwSize > 0)
{
TCHAR lpszValue[MAX_PATH] = _T("");
LONG lRes = ::RegQueryValueEx(hachKey, _T("NetCfgInstanceId"), NULL, &dwType, (LPBYTE)lpszValue, &dwSize);
if (_tcscmp(lpszValue, strAdapterName) == 0) {
bFind = TRUE;
dwType = REG_DWORD;
//dwSize = sizeof(DWORD);
LONG lRes = ::RegQueryValueEx(hachKey, _T("Characteristics"), NULL, &dwType, (LPBYTE)&dwValue, &dwSize);
if (dwValue & 0x04){
nRet = TRUE;
}
}
}
::RegCloseKey(hachKey);
if (bFind || nRet) {
break;
}
}
}
}
}
::RegCloseKey(hSubKey);
return nRet;
}
int GetPhysicalMacStr(CStringArray &macList)
{
macList.RemoveAll();
ULONG len = 0;
if (GetAdaptersInfo(NULL, &len) != ERROR_BUFFER_OVERFLOW || len == 0) {
ASSERT(FALSE);
return 0;
}
IP_ADAPTER_INFO* pAdapterBase = (IP_ADAPTER_INFO*)new BYTE[len];
if (pAdapterBase != NULL) {
DWORD dwError = GetAdaptersInfo(pAdapterBase, &len);
if (dwError == ERROR_SUCCESS) {
IP_ADAPTER_INFO* pAdapterInfo = pAdapterBase;
while (pAdapterInfo != NULL)
{
if (IsPhysicalMac(pAdapterInfo->AdapterName)) {
CString strMac,strFmt;
for (int i = 0; i < pAdapterInfo->AddressLength; i++) {
strFmt.Format(_T("%.2X-"), pAdapterInfo->Address[i]);
strMac += strFmt;
}
strMac.TrimRight('-');
macList.Add(strMac);
}
pAdapterInfo = pAdapterInfo->Next;
}
}
else {
ASSERT(FALSE);
}
delete[](BYTE*)pAdapterBase;
}
return macList.GetSize();
}
[code]原文链接:https://blog.csdn.net/hzy888cp/article/details/98180422[/code] |
|