如何使用 C++ 在 Windows 中获取 MAC 地址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13646621/
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 MAC Address in Windows with C++?
提问by guiarrd
I need some help with MAC addresses. I have to get it by using some code in C++ so could anybody help me with this? I've already tried a lot of useless codes. If exists any specific method or lib that I should study to find the MAC address, I will be very happy if anybody pass me a link or something to know more about this.
我需要一些有关 MAC 地址的帮助。我必须通过在 C++ 中使用一些代码来获得它,所以有人可以帮助我吗?我已经尝试了很多无用的代码。如果存在任何我应该研究的特定方法或库来查找 MAC 地址,如果有人向我传递链接或其他内容以了解更多信息,我将非常高兴。
回答by guiarrd
I got it people! Me and a guy from the work solve this using this code:
我明白了人!我和一个工作人员使用以下代码解决了这个问题:
#include <stdio.h>
#include <Windows.h>
#include <Iphlpapi.h>
#include <Assert.h>
#pragma comment(lib, "iphlpapi.lib")
char* getMAC();
int main(){
char* pMac = getMAC();
system("pause");
free(pMac);
}
char* getMAC() {
PIP_ADAPTER_INFO AdapterInfo;
DWORD dwBufLen = sizeof(IP_ADAPTER_INFO);
char *mac_addr = (char*)malloc(18);
AdapterInfo = (IP_ADAPTER_INFO *) malloc(sizeof(IP_ADAPTER_INFO));
if (AdapterInfo == NULL) {
printf("Error allocating memory needed to call GetAdaptersinfo\n");
free(mac_addr);
return NULL; // it is safe to call free(NULL)
}
// Make an initial call to GetAdaptersInfo to get the necessary size into the dwBufLen variable
if (GetAdaptersInfo(AdapterInfo, &dwBufLen) == ERROR_BUFFER_OVERFLOW) {
free(AdapterInfo);
AdapterInfo = (IP_ADAPTER_INFO *) malloc(dwBufLen);
if (AdapterInfo == NULL) {
printf("Error allocating memory needed to call GetAdaptersinfo\n");
free(mac_addr);
return NULL;
}
}
if (GetAdaptersInfo(AdapterInfo, &dwBufLen) == NO_ERROR) {
// Contains pointer to current adapter info
PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;
do {
// technically should look at pAdapterInfo->AddressLength
// and not assume it is 6.
sprintf(mac_addr, "%02X:%02X:%02X:%02X:%02X:%02X",
pAdapterInfo->Address[0], pAdapterInfo->Address[1],
pAdapterInfo->Address[2], pAdapterInfo->Address[3],
pAdapterInfo->Address[4], pAdapterInfo->Address[5]);
printf("Address: %s, mac: %s\n", pAdapterInfo->IpAddressList.IpAddress.String, mac_addr);
// print them all, return the last one.
// return mac_addr;
printf("\n");
pAdapterInfo = pAdapterInfo->Next;
} while(pAdapterInfo);
}
free(AdapterInfo);
return mac_addr; // caller must free.
}
回答by unwind
C++ doesn't have any built-in concept of a "MAC address", it's not something that has to exist in order for C++ code to run. Thus, it's platform-specific. You must tell us which platform you're trying to do this for, and also (of course) read documentation that matches that platform.
C++ 没有任何内置的“MAC 地址”概念,它不是为了运行 C++ 代码而必须存在的东西。因此,它是特定于平台的。您必须告诉我们您要针对哪个平台执行此操作,并且(当然)还必须阅读与该平台匹配的文档。
If you want to do this in a portable way, you should look for a suitable library that supports all the desired platforms.
如果您想以可移植的方式执行此操作,您应该寻找支持所有所需平台的合适库。
Also, note that a computer can have any number of network adapters, so there's no requirement that there is just oneMAC address.
另请注意,一台计算机可以有任意数量的网络适配器,因此不需要只有一个MAC 地址。