Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Get MAC address of network adapters on Vista?

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!

like image 532
Laur Avatar asked Oct 21 '08 13:10

Laur


People also ask

How do I find my MAC address Windows Vista?

For Windows Vista/7: 2. In the Command Prompt window, type ipconfig /all and press Enter. Under the Ethernet Adapter Local Area Connection section, look for the "Physical Address". This is your MAC Address.

What is my network interface MAC?

Mac OS X 10.9 or later Click on the Apple icon in the top left, and click on System Preferences, or open System Preferences from your Dock. In the System Preferences window, click on Network. In the resulting network window, there will be network interfaces listed on the left.


2 Answers

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;

    }
}
like image 53
Brian R. Bondy Avatar answered Oct 12 '22 23:10

Brian R. Bondy


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

like image 20
graham.reeds Avatar answered Oct 13 '22 00:10

graham.reeds