Winsock Programmer's FAQ
Example: How to Get the Ethernet MAC Address, RPC Method

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

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

  • When the DCOM 1.3 upgrade for Windows 95 is installed. Windows 95 with DCOM 1.2, Windows 98 and Windows 98 SE are apparently not affected.
  • On all versions of Windows 2000.
  • When there are 0 or 2+ Ethernet adapters in the system.

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

#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 Get MAC Address, SNMP Method >>
Last modified on 17 October 2001 at 16:26 UTC-7 Please send corrections to tangent@cyberport.com.
< Go to the main FAQ page << Go to the Home Page