<<

Winsock Programmer's FAQ
Examples: Get 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 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:

  • 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. (And presumably its successors.)
  • 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
//
// 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 Sat Jun 11 2005 00:32 MDT   Go to my home page