前段时间有个项目需要获取客户端的 MAC 地址,用作统计去重的参考数据。从网上查到的获取 MAC 地址的代码,大多是用同一段代码修改的。于是我也用了那段代码。代码如下:
- void GetMAC(BYTE mac[BUF_SIZE])
- {
- ULONG size_pointer;
- PIP_ADAPTER_INFO pip_adapter_info = NULL;
- if(ERROR_BUFFER_OVERFLOW != GetAdaptersInfo(NULL, &size_pointer))
- {
- wsprintfA((LPSTR)mac, "GetMAC Failed! ErrorCode: %d", GetLastError());
- return;
- }
- pip_adapter_info = (PIP_ADAPTER_INFO)calloc(size_pointer, sizeof(BYTE));
- if(NULL == pip_adapter_info)
- {
- lstrcpyA((LPSTR)mac, "GetMAC Failed! Because calloc failed!");
- return;
- }
- if(ERROR_SUCCESS != GetAdaptersInfo(pip_adapter_info, &size_pointer))
- {
- wsprintfA((LPSTR)mac, "GetMAC Failed! ErrorCode: %d", GetLastError());
- free(pip_adapter_info);
- return;
- }
- for(int i = 0; i < 6; ++i)
- {
- wsprintfA((LPSTR)mac, "%02X", pip_adapter_info->Address[i]);
- mac += 2;
- }
- }
在自己的电脑上、虚拟机上测试了一番,没有发现问题,觉得一切 OK 之后将产品发布出去,结果发现许多机器返回的 MAC 为 NULL!!!
问题严重,又重新研究了一番,发现之所以 MAC 返回 NULL 是因为这段代码并不完整:用 GetAdaptersInfo 获取到的不一定是网卡信息,有可能是别的适配器信息,不是网卡就没有 MAC,结果当然为 NULL!花了点时间把代码完善了一下,加了个检测适配器特性的函数,完成了该功能,最终的代码如下:
- #include <windows.h>
- #include <iphlpapi.h> // API GetAdaptersInfo 头文件
- #include <shlwapi.h> // API StrCmpIA 头文件
- #pragma comment(lib, "iphlpapi.lib")
- #pragma comment(lib, "shlwapi.lib")
- #include <Strsafe.h> // API StringCbPrintfA 头文件
- #include <shellapi.h> // API lstrcpyA 头文件
- //
- // 功能:获取适配器特性
- // 参数:
- // adapter_name 适配器 ID
- // 返回值:成功则返回由参数指定的适配器的特性标志,是一个 DWORD 值,失败返回 0
- //
- UINT GetAdapterCharacteristics(char* adapter_name)
- {
- if(adapter_name == NULL || adapter_name[0] == 0)
- return 0;
- HKEY root = NULL;
- // 打开存储适配器信息的注册表根键
- if(ERROR_SUCCESS != RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}", 0, KEY_READ, &root))
- return 0;
- DWORD subkeys = 0;
- // 获取该键下的子键数
- if(ERROR_SUCCESS != RegQueryInfoKeyA(root, NULL, NULL, NULL, &subkeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL))
- subkeys = 100;
- DWORD ret_value = 0;
- for(DWORD i = 0; i < subkeys; i++)
- {
- // 每个适配器用一个子键存储,子键名为从 0 开始的 4 位数
- char subkey[MAX_SIZE];
- memset(subkey, 0, MAX_SIZE);
- StringCbPrintfA(subkey, MAX_SIZE, "%04u", i);
- // 打开该子键
- HKEY hKey = NULL;
- if(ERROR_SUCCESS != RegOpenKeyExA(root, subkey, 0, KEY_READ, &hKey))
- continue;
- // 获取该子键对应的适配器 ID,存于 name 中
- char name[MAX_PATH];
- DWORD type = 0;
- DWORD size = MAX_PATH;
- if(ERROR_SUCCESS != RegQueryValueExA(hKey, "NetCfgInstanceId", NULL, &type, (LPBYTE)name, &size))
- {
- RegCloseKey(hKey);
- continue;
- }
- // 对比该适配器 ID 是不是要获取特性的适配器 ID
- if(StrCmpIA(name, adapter_name) != 0)
- {
- RegCloseKey(hKey);
- continue;
- }
- // 读取该适配器的特性标志,该标志存储于值 Characteristics 中
- DWORD val = 0;
- size = 4;
- LSTATUS ls = RegQueryValueExA(hKey, "Characteristics", NULL, &type, (LPBYTE)&val, &size);
- RegCloseKey(hKey);
- if(ERROR_SUCCESS == ls)
- {
- ret_value = val;
- break;
- }
- }
- RegCloseKey(root);
- return ret_value;
- }
- //
- // 功能:获取 Mac 地址的二进制数据
- // 参数:
- // mac 用于输出 Mac 地址的二进制数据的缓冲区指针
- // 返回值:成功返回 mac 地址的长度,失败返回 0,失败时 mac 中保存一些简单的错误信息,可适当修改,用于调试
- //
- int GetMAC(BYTE mac[BUF_SIZE])
- {
- #define NCF_PHYSICAL 0x4
- DWORD AdapterInfoSize = 0;
- if(ERROR_BUFFER_OVERFLOW != GetAdaptersInfo(NULL, &AdapterInfoSize))
- {
- StringCbPrintfA((LPSTR)mac, BUF_SIZE, "GetMAC Failed! ErrorCode: %d", GetLastError());
- return 0;
- }
- void* buffer = malloc(AdapterInfoSize);
- if(buffer == NULL)
- {
- lstrcpyA((LPSTR)mac, "GetMAC Failed! Because malloc failed!");
- return 0;
- }
- PIP_ADAPTER_INFO pAdapt = (PIP_ADAPTER_INFO)buffer;
- if(ERROR_SUCCESS != GetAdaptersInfo(pAdapt, &AdapterInfoSize))
- {
- StringCbPrintfA((LPSTR)mac, BUF_SIZE, "GetMAC Failed! ErrorCode: %d", GetLastError());
- free(buffer);
- return 0;
- }
- int mac_length = 0;
- while(pAdapt)
- {
- if(pAdapt->AddressLength >= 6 && pAdapt->AddressLength <= 8)
- {
- memcpy(mac, pAdapt->Address, pAdapt->AddressLength);
- mac_length = pAdapt->AddressLength;
- UINT flag = GetAdapterCharacteristics(pAdapt->AdapterName);
- bool is_physical = ((flag & NCF_PHYSICAL) == NCF_PHYSICAL);
- if(is_physical)
- break;
- }
- pAdapt = pAdapt->Next;
- }
- free(buffer);
- return mac_length;
- }
- //
- // 功能:获取 Mac 地址,使用时直接调用此函数即可
- // 参数:
- // mac 用于存储 Mac 地址的缓冲区指针
- // 返回值:无返回值,函数执行完后会把 Mac 地址以16进制的形式存于参数指定的缓冲区中,若有错误,缓冲区中保存的是错误信息
- //
- void GetMacAddress( char* mac )
- {
- BYTE buf[BUF_SIZE];
- memset(buf, 0, BUF_SIZE);
- int len = GetMAC(buf);
- if(len <= 0)
- {
- lstrcpyA(mac, (LPCSTR)buf);
- return;
- }
- if(len == 6)
- StringCbPrintfA(mac, BUF_SIZE, "%02X-%02X-%02X-%02X-%02X-%02X", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
- else
- StringCbPrintfA(mac, BUF_SIZE, "%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
- }
编译环境: VS2008 + Windows SDK 7.1
函数功能在 Windows 2000、Windows XP、Windows 2003、Vista、 Win7 32位和 Win7 64 位下均测试通过。