views:

2580

answers:

4

We are currently using the NetBios method, and it works ok under XP. Preliminary tests under Vista show that it also works, but there are caveats - NetBIOS has to be present, for instance, and from what I've been reading, the order of the adapters is bound to change. Our alternative method - with SNMPExtensionQuery - seems to be broken under Vista.

The question is: do you know of a reliable way to get a list of the local MAC addresses on a Vista machine? Backwards compatibility with XP is a plus (I'd rather have one single method than lots of ugly #ifdef's). Thanks!

+1  A: 

GetAdaptersInfo() is the official method, it enumerates all adapters even ones that are disabled.
See this post for example code codeguru

Martin Beckett
Incorrect. GetAdaptersInfo() can not enumerate adapters that are disabled. The code guru article even states this fact:"Finally it also works if your NICs are not connected to valid networks (eg. wires are not even hooked up), but the NICs do have to be "enabled" in Windows"
0xC0DEFACE
A: 

You can use WMI on both XP and Vista, there are a number of examples online. e.g: Use Windows Management Instrumentation (WMI) to get a MAC Address

Rob Walker
+2  A: 

Could you use the WMIService? I used it to get the mac-address of a machine in pre-Vista days though.

graham.reeds
Thanks, this seems to be the cleanest solution to my problem.
Laur
+8  A: 

This will give you a list of all MAC addresses on your computer. It will work with all versions of Windows as well:

void getdMacAddresses(std::vector<std::string> &vMacAddresses;)
{
    vMacAddresses.clear();
    IP_ADAPTER_INFO AdapterInfo[32];       // Allocate information for up to 32 NICs
    DWORD dwBufLen = sizeof(AdapterInfo);  // Save memory size of buffer
    DWORD dwStatus = GetAdaptersInfo(      // Call GetAdapterInfo
    AdapterInfo,                 // [out] buffer to receive data
    &dwBufLen);                  // [in] size of receive data buffer

    //No network card? Other error?
    if(dwStatus != ERROR_SUCCESS)
     return;

    PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;
    char szBuffer[512];
    while(pAdapterInfo)
    {
     if(pAdapterInfo->Type == MIB_IF_TYPE_ETHERNET)
     {
      sprintf_s(szBuffer, sizeof(szBuffer), "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x"
       , pAdapterInfo->Address[0]
       , pAdapterInfo->Address[1]
       , pAdapterInfo->Address[2]
       , pAdapterInfo->Address[3]
       , pAdapterInfo->Address[4]
       , pAdapterInfo->Address[5]
       );
      vMacAddresses.push_back(szBuffer);
     }
     pAdapterInfo = pAdapterInfo->Next;

    }
}
Brian R. Bondy
Hi Brian,Thanks for the heads-up; in the meantime I found this link (for XP and later); I guess I'll go either for this or for the WMI solution.http://msdn.microsoft.com/en-us/library/aa365915(VS.85).aspx
Laur
We've used this method above in our main products for several years. Works good in Vista, 2008, 2003, XP, 2000, ....
Brian R. Bondy
Update: and win7 :)
Brian R. Bondy