【转载】 用 Windows API “GetAdaptersInfo” 获取 MAC 时遇到的问题


  From:http://blog.csdn.net/weiyumingwww/article/details/17554461

前段时间有个项目需要获取客户端的 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、WindowsXP、Windows 2003、Vista、 Win7 32位和 Win7 64 位下均测试通过。

优质内容筛选与推荐>>
1、vim tutor summary
2、微信公众号系统在Linux下的部署操作
3、面试-23种设计模式
4、apache代理服务器设置
5、git学习笔记


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn