C++ 如何在 Windows 上获取硬件 MAC 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/823553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get hardware MAC address on Windows
提问by Mark
I'm playing around with retrieving the MAC address from the NIC - there are a variety of ways to get it, this article covers the most common:
我正在尝试从 NIC 检索 MAC 地址 - 有多种方法可以获取它,本文介绍了最常见的:
http://www.codeguru.com/Cpp/I-N/network/networkinformation/article.php/c5451
http://www.codeguru.com/Cpp/IN/network/networkinformation/article.php/c5451
I'm currently using the GetAdaptersInfo method, which seems the most bulletproof, but if the MAC address has been set via the registry:
我目前正在使用 GetAdaptersInfo 方法,这似乎是最安全的方法,但如果 MAC 地址已通过注册表设置:
Then it reports the MAC address that it has been changed to. The only way I've found to actually get the true MAC is to remove the registry entry, restart the NIC, get the MAC via GetAdaptersInfo, then replace the registry entry, and restart the NIC. While it gets the job done, it's hardly transparent to the user.
然后它报告它已更改为的 MAC 地址。我发现真正获得真正 MAC 的唯一方法是删除注册表项,重新启动 NIC,通过 GetAdaptersInfo 获取 MAC,然后替换注册表项,然后重新启动 NIC。虽然它完成了工作,但它对用户来说几乎不透明。
Is there any other methods that anyone is familiar with, that will return the hardware MAC regardless of what the registry is set to? Ideally I'd like a solution that works on XP on up.
是否有任何其他人熟悉的方法,无论注册表设置为什么,都会返回硬件 MAC?理想情况下,我想要一个可以在 XP 上运行的解决方案。
Thanks in advance!
提前致谢!
采纳答案by SmacL
My guess is that in the linked CodeGuru article, the Miniport solution is likely to overcome the problem you describe, albeit painful to implement. The reason I think this is that I have used the GetAdaptersInfo solution myself in the past, and noticed that the MAC address will change without reboot when an adapter is added, e.g. a Bluetooth adapter providing PAN services.
我的猜测是,在链接的 CodeGuru 文章中,Miniport 解决方案可能会克服您描述的问题,尽管实施起来很痛苦。我认为这是因为我过去自己使用过 GetAdaptersInfo 解决方案,并注意到添加适配器(例如提供 PAN 服务的蓝牙适配器)时,MAC 地址会更改而无需重新启动。
Perhaps rather than rebooting after changing the registry setting, you could try stopping and restarting the relevent network services. You could easily check this manually prior to looking for a programmatic solution.
也许不是在更改注册表设置后重新启动,您可以尝试停止并重新启动相关的网络服务。在寻找程序化解决方案之前,您可以轻松地手动检查这一点。
(n.b. the above is all guess work. If you try it and it works, perhaps add a post for those trying to do the same in future).
(注意以上都是猜测。如果您尝试并有效,也许为将来尝试做同样事情的人添加一个帖子)。
回答by codeape
Parse the output of ipconfig /all
解析输出 ipconfig /all
回答by donovan
You can use WMI to enumerate the Win32_NetworkAdapterinstances and look at the MACAddress property. The main issue with this technique is finding the appropriate adapter instance if you have multiple active adapters installed, e.g. on a laptop which also has a wireless connection.
您可以使用 WMI 枚举Win32_NetworkAdapter实例并查看 MACAddress 属性。如果您安装了多个活动适配器,例如在也有无线连接的膝上型电脑上,则此技术的主要问题是找到合适的适配器实例。