This example relies on a property of the UUIDs used by Windows’ RPC mechanism. While this property is not guaranteed to exist, it’s suggested by the RPC spec, and has worked in all Microsoft RPC implementations until relatively recently.

Between Microsoft’s rescinding of the feature in newer systems and the feature’s inherent unreliability, the following code will fail to work properly in the following situations:

For these reasons, I have to recommend that you do not use this feature in your programs. This code is still here for historical reasons, and as a formal warning. There are two other methods here in the FAQ’s examples section that do work, and there is a FAQ item that points you to a few other methods. There is no good reason to use this method.


getmac-rpc.cpp

// Visual C++ 5.0:  cl -GX getmac-rpc.cpp rpcrt4.lib
// Borland C++ 5.0: bcc32 getmac-rpc.cpp
//
// This sample program is hereby placed in the public domain.

#include <rpc.h>
#include <iostream>

#ifdef _MSC_VER
using namespace std;
#endif

int main()
{
    cout << "MAC address is: ";

    // Ask RPC to create a UUID for us.  If this machine has an Ethernet
    // adapter, the last six bytes of the UUID (bytes 2-7 inclusive in
    // the Data4 element) should be the MAC address of the local
    // Ethernet adapter.
    UUID uuid;
    UuidCreate(&uuid);

    // Spit the address out
    for (int i = 2; i < 8; ++i) {
        cout << hex;
        cout.fill('0');
        cout.width(2);
        cout << int (uuid.Data4[i]);
        if (i < 7) {
            cout << ":";
        }
    }
    cout << endl;

    return 0;
}


<< Get MAC Address, NetBIOS Method
Get MAC Address, SNMP Method >>
Updated Sun Jan 18 2015 04:24 MST   Go to my home page