Getting Machine's MAC Address -- Good Solution?
Asked Answered
B

3

5

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".

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

Bellyband answered 15/1, 2010 at 6:28 Comment(1)
Good solution for what? If you mean for generating some unique computer identifier for licensing I would say it should not be relied on. What happens when user changes network card, has 2 cards, has no network card?Verdie
M
6

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.

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;
}

Please forgive the ancient C code...

Machellemachete answered 15/1, 2010 at 7:48 Comment(1)
This was working in a project by itself but in the project with winpcap I was trying to utilize it in it had lots of compiler errors in netioapi.hBellyband
T
5

There's no such thing as "the" MAC address if a computer has >1 NIC. You can query this information using WMI, http://techsupt.winbatch.com/TS/T000001002F19.html for more details

Trona answered 15/1, 2010 at 6:31 Comment(0)
M
0

IPHelper's GetAdaptersAddresses()
http://msdn.microsoft.com/en-us/library/aa365915%28VS.85%29.aspx
I use successfully on WinCE to get operstatus, should work with MAC Address too.

Malinin answered 15/1, 2010 at 8:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.