Click here to Skip to main content
13,298,965 members (40,229 online)
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 25 Mar 2007

Getting the Network Adaptor MAC Address with WMI

, 25 Mar 2007
Rate this:
Please Sign up or sign in to vote.
Getting the network adaptor MAC address with WMI


This sample piece of code has two purposes:

  1. It will give you a general background on why to use all this rich set of WMI classes in C++.
  2. It will demonstrate an alternative method for obtaining a MAC address of the network card of the computer.


I stumbled upon this problem while I was using the RPC function UuidCreateSequential from Platform SDK when suddenly on several computers it began to give different MACs every time. Then I found this statement on MSDN:

"For security reasons, UuidCreate was modified so that it no longer uses a machine's MAC address to generate UUIDs."

So I had to find an alternative method. One was to use the NetBIOS function, but firstly, it's quite complicated and secondly, not every machine has NetBIOS installed. So I turned to the WMI alternative which turned to be quite simple.

Using the Code

In the attached ZIP file, you will find a console application that just prints out all the network cards MACs. All the code is in the main function, so here it is with some explanations:

int _tmain(int argc, _TCHAR* argv[])
    CoInitialize(NULL);    // Initialize COM 
        //Create the locator object through which we will 
        //access all the other WMI functions
        WbemScripting::ISWbemLocatorPtr locator;
        if (locator != NULL)
            // Get the pointer to the WMI service on the active computer 
	   // (the local machine)
            WbemScripting::ISWbemServicesPtr services = 
            // Get the list of objects of interest, in our case the network adaptors
            WbemScripting::ISWbemObjectSetPtr objects = 
		services->ExecQuery("Select * from Win32_NetworkAdapter",
            // Create the enumerator for the collection 
            IEnumVARIANTPtr obj_enum = objects->Get_NewEnum();             
            ULONG fetched;            
            VARIANT var;
            // Iterate through the collection
            while (obj_enum->Next(1,&var,&fetched) == S_OK)
                // Get an object (an instance of Network adaptor WMI class)
                WbemScripting::ISWbemObjectPtr object = var;
                // Retrieve the properties
                WbemScripting::ISWbemPropertySetPtr properties = object->Properties_;
                // Check the adaptor's type by retrieving the "AdapterTypeID" property
                WbemScripting::ISWbemPropertyPtr prop = 
                _variant_t value = prop->GetValue();
                if (value.vt == VT_I4 && (int)value == 0) // If LAN adaptor
                    //Retrieve the "MACAddress" property
                    prop = properties->Item("MACAddress",0);
                    // And print it out
                    printf("MAC address found: %s\n",
			(const char*)_bstr_t(prop->GetValue()));                
    // In case there was an error, print it out...
    catch (_com_error err)
        printf("Error occurred: %S",err.ErrorMessage());
    return 0;


  • 25th March, 2007: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Alex Hazanov
Software Developer (Senior) RDV Systems
Israel Israel
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
fkeujjpdc20-Dec-10 3:19
memberfkeujjpdc20-Dec-10 3:19 
GeneralThanks a bunch Pin
kvrnkiran15-Jun-07 0:28
memberkvrnkiran15-Jun-07 0:28 
GeneralNice Post Pin
alex turner25-Mar-07 23:08
memberalex turner25-Mar-07 23:08 
GeneralYou don't need WMI to get the MAC address Pin
jonnybgood225-Mar-07 13:48
memberjonnybgood225-Mar-07 13:48 
GeneralRe: You don't need WMI to get the MAC address Pin
Alex Hazanov25-Mar-07 22:33
memberAlex Hazanov25-Mar-07 22:33 
GeneralHmmm Pin
Graham Shanks25-Mar-07 9:01
memberGraham Shanks25-Mar-07 9:01 
AnswerRe: Hmmm Pin
Ravi Bhavnani25-Mar-07 14:28
memberRavi Bhavnani25-Mar-07 14:28 
GeneralRe: Hmmm Pin
Alex Hazanov25-Mar-07 22:37
memberAlex Hazanov25-Mar-07 22:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 25 Mar 2007
Article Copyright 2007 by Alex Hazanov
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid