获取机器的MAC地址-好的解决方案? [英] Getting Machine's MAC Address -- Good Solution?

查看:234
本文介绍了获取机器的MAC地址-好的解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说我当前的winpcap库是不可能的.

I've heard it's not possible with my current library of winpcap.

这是真的吗?我在网上看到很多示例,但随后评论说这不起作用".

Is this really true? I see lots of examples on the net but then comments saying "This doesn't work".

获取本地计算机的MAC地址的最佳方法是什么?

What's the best way to get a MAC address of the local machine?

推荐答案

一种常见的方法是使用UUID中的位,但这并不完全可靠.例如,即使在没有网络适配器的计算机上,它也会返回一个值.

One common method is using bits from a UUID, but this isn't entirely dependable. For example, it'll return a value even on a machine that doesn't have a network adapter.

幸运的是,有一种方法可以在任何合理的Windows最新版本上可靠地工作. MSDN表示,它只能追溯到Windows 2000,但如果有内存可用,它也可以在NT 4上运行,从SP 5开始,以防万一有人还在使用NT 4.

Fortunately, there is a way that works dependably on any reasonably recent version of Windows. MSDN says it only goes back to Windows 2000, but if memory serves, it also works on NT 4, starting around SP 5, in case anybody's still using NT 4.

#include <windows.h>
#include <iphlpapi.h>
#include <stdio.h>

int main() {         
    IP_ADAPTER_INFO *info = NULL, *pos;
    DWORD size = 0;

    GetAdaptersInfo(info, &size);

    info = (IP_ADAPTER_INFO *)malloc(size);

    GetAdaptersInfo(info, &size);

    for (pos=info; pos!=NULL; pos=pos->Next) {
        printf("\n%s\n\t", pos->Description);
        printf("%2.2x", pos->Address[0]);
        for (int i=1; i<pos->AddressLength; i++)
            printf(":%2.2x", pos->Address[i]);
    }

    free(info);
    return 0;
}

请原谅古老的C代码...

Please forgive the ancient C code...

这篇关于获取机器的MAC地址-好的解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆