Windows

Getting the network adaptor MAC address with WMI

_침묵_ 2007. 3. 29. 20:03

출처 : http://www.codeproject.com/useritems/MAC_address_throuh_WMI.asp

 

Introduction

This sample piece of code has two purposes:

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

Background

I have stambled on this problem while I was using the RPC functionUuidCreateSequentialfrom Platform SDK when suddenly on several computers it began to give different MACs every time than 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 NetBIOS function , but first: it's quite complicated and second: not every machine has NetBIOS installed, so I turned to the WMI alternative wich turned to be quite simple

Using the code

In the attached ZIP file you will find a console applicatio that just print's out all the network card's MACs , all the code is in the main function , so here it is with some explanations:

History

사용자 삽입 이미지
Collapse
int_tmain(intargc, _TCHAR* argv[]){    CoInitialize(NULL);// Initialize COMtry{//Create the locator object through with we will access alll the other WMI functionsWbemScripting::ISWbemLocatorPtr locator;                locator.CreateInstance(WbemScripting::CLSID_SWbemLocator);if(locator != NULL)        {// Get the pointer to the WMI service on the active computer (the local mashine)WbemScripting::ISWbemServicesPtr services = locator->ConnectServer(".","root\\cimv2","","","","",0,NULL);// Get thee list of objects of interest , in our case the network adaptorsWbemScripting::ISWbemObjectSetPtr objects = services->ExecQuery("Select * from Win32_NetworkAdapter","WQL",0x10,NULL);// Create the enumerator for the collectionIEnumVARIANTPtr obj_enum = objects->Get_NewEnum();                         ULONG fetched;                        VARIANT var;// Iterate through the collectionwhile(obj_enum->Next(1,&var,&fetched) == S_OK)            {// Get an object (an instance of Network adaptor WMI class)WbemScripting::ISWbemObjectPtr object = var;// Retrieve the propertiesWbemScripting::ISWbemPropertySetPtr properties = object->Properties_;// Chesk the adaptor's type by retrieving the "AdapterTypeID" propertyWbemScripting::ISWbemPropertyPtr prop = properties->Item("AdapterTypeID",0);                _variant_t value = prop->GetValue();if(value.vt == VT_I4 && (int)value ==0)// If LAN adaptor{//Retrieve the "MACAddress" propertyprop = properties->Item("MACAddress",0);// And print it outprintf("MAC address found: %s\n",(constchar*)_bstr_t(prop->GetValue()));                                }            }        }    }// In case there was an error print it out...catch(_com_error err)    {        printf("Error ocured: %S",err.ErrorMessage());    }    CoUninitialize();return0;}

About Alex Hazanov

사용자 삽입 이미지

Alex is a software engineer specializing in software development for CAD and engineering industry.
Alex was born in Moscow - Russia and lives in Israel since 1990.
Currently he is software development team manager in a small comany - Meidan Engineering and Computers LTD. which developes products on platforms like Autodesk's AutoCAD targeted for Microsoft Windows. Alex is experienced with technologies as Microsoft C++, Visual Basic, XML Java Script, HTML , ASP, ObjectARX, Lisp. Alex also worked as computer technitian and sysadmin in the past so he is familiar with hardware and networking as well as software.

Clickhereto view Alex Hazanov's online profile.