Reputation: 1119
I am using the following code to get the MAC address:
IP_ADAPTER_INFO adpInfo[16];
DWORD len = sizeof(adpInfo);
GetAdaptersInfo(adpInfo, &len );
printf("%02x%02x%02x%02x%02x%02x", adpInfo[0].Address[0], adpInfo[0].Address[1], adpInfo[0].Address[2], adpInfo[0].Address[3], adpInfo[0].Address[4], adpInfo[0].Address[5]);
However, if the computer has many network adapters (for example: Ethernet and WiFi), then every time I call this code I get a different MAC address.
Is there a way to always get the same MAC address (for example: Ethernet).
Upvotes: 5
Views: 3117
Reputation: 3134
I believe enumeration of the network adaptor information by the windows os depends on the priority of the network adaptors. Priorities of network adapters can be viewed ,edited by traversing to
Open Network and Sharing Centre -> Change adapter settings ->Advanced[Enable menu bar if not visible]->Advanced settings.
One can edit the priorities of the network adapter.
Upvotes: 1
Reputation: 4655
Since GetAdaptersInfo method includes almost as much information as IPCONFIG /ALL (including your DHCP server, Gateway, IP address list, subnet mask and WINS server) you can use that. It also enumerates all the NICs on your PC, even if they are not connected to valid networks (but the NICs do have to be "enabled" in Windows)
Sample, print all interfaces:
static void GetMACaddress(void)
{
IP_ADAPTER_INFO AdapterInfo[16];
DWORD dwBufLen = sizeof(AdapterInfo);
DWORD dwStatus = GetAdaptersInfo(AdapterInfo, &dwBufLen);
assert(dwStatus == ERROR_SUCCESS);
PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;
do {
PrintMACaddress(pAdapterInfo->Address);
pAdapterInfo = pAdapterInfo->Next;
}
while(pAdapterInfo);
}
You can save the AdapterName, then compare it in next calls to make sure the MAC of specified adapter is retrieved.
Look at here for IP_ADAPTER_INFO structure: https://msdn.microsoft.com/en-us/library/windows/desktop/aa366062%28v=vs.85%29.aspx
Upvotes: 3