Click here to Skip to main content
11,411,922 members (66,094 online)
Click here to Skip to main content

Getting Information from WMI in Visual C++

, 27 Sep 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
This is an article just to describe how to use WMI with Visual C++ 6. I had to do this for one of my projects and I finally came up with this solution. I hope this will be beneficial to others as well.


We normally find a lot of ways and a number of resources to use WMI or to get information from “Windows Management Instrumentation” while using Visual Basic 6 and C#, but I could not find a single resource describing the same thing in Visual C++. MSDN resources are also very limited in this context.


Following is the code on how to get the current processor load from a WMI class Win32_Processor defined in a .mof file. .mof files are managed object files which have a typical way of defining classes and methods.

WMI provides the COM service which is used to connect to the WMI services. The important parts of the code include:

  • bstrNamespace: The initialization of this variable is very tricky. The first three forward slashes //./ represent the Host Computer name from which you want to get information from. A “.” Indicates that information is to be obtained from the same computer on which you are working. You can give any network name here but getting information from the network depends upon your Access Rights, etc. cimv2 is the namespace containing the Win32_Processor class.
  • pIWbemLocator is the argument in which we get the Interface pointer.
  • After that, we call the function ConnectServer of the pIWbemLocator to get a pointer to pWbemServices.
  • WMI uses its own Query Language to get information known as WQL (the acronym for WMI Query Language). So, when calling the function ExecQuery, we have to specify the language as its first argument. Second argument is the Query itself. Last argument is important because here we get a pointer to an Enumeration object through which we can enumerate through the objects available. This enumeration is important because consider the case that we want to know the information about running processes and we are using Win32_Process class for this purpose. Then through this enumeration object, we can go through different processes one by one.
  • By calling the Reset and Next methods of pEnumObject, we are moving through the objects. We get the pointer to an object in pClassObject.
  • The last function through which we get the actual value of a property is Get. We pass a BSTR to this function to get the value in a variant.

The code for getting Processor Load is in a thread which keeps on running and posts message to UI to update the Progress bar. Here is the Thread function.

UINT GetProcessorLoad( LPVOID pParam )
	CUsingWMIDlg* dlg = (CUsingWMIDlg*)pParam;

	HRESULT hRes = CoInitializeSecurity( NULL, -1, NULL, 
		EOAC_NONE, 0);

	if(hRes != S_OK) return 0;

	CComPtr<iwbemlocator> pIWbemLocator;
	CComPtr<iwbemservices> pIWbemServices;
	CComPtr<ienumwbemclassobject> pIEnumObject;

	CComBSTR bstrNamespace(_T("root\\cimv2"));

	hRes = pIWbemLocator.CoCreateInstance(CLSID_WbemAdministrativeLocator);
	if(hRes != S_OK) return 0;

	hRes = pIWbemLocator->ConnectServer(bstrNamespace, NULL, 
		NULL, NULL, 0, NULL, NULL, &pIWbemServices);
	if(hRes != S_OK) return 0;

		CComBSTR bstrQuery(_T("Select * from Win32_Processor"));
		CComBSTR bstrQL(_T("WQL"));

		hRes = pIWbemServices->ExecQuery
		(bstrQL, bstrQuery, WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pIEnumObject);

		ULONG uCount = 1, uReturned;
		CComPtr<iwbemclassobject> pIClassObject;

		hRes = pIEnumObject->Reset();

		hRes = pIEnumObject->Next
		(WBEM_INFINITE, uCount, &pIClassObject, &uReturned);

		if(hRes != S_OK) return 0;

		CComVariant var;

		CComBSTR bstrProp(_T("LoadPercentage"));

		hRes = pIClassObject->Get(bstrProp, 0, &var, NULL, NULL);
		if(hRes != S_OK) return 0;

		int * value = new int(var.lVal);
		dlg->PostMessage(WM_USER+1, (WPARAM) value);
	return 1;


This was the shortest method I was able to work out to get information from any WMI class. You can simply change the class name in the Query and Property Name while calling Get method and you will get information from all the classes supported in your OS. I have now tested this code in Windows 7, Windows XP, Win2000, Win2003 and Win2008.


I found out that the code associated with this article was very badly written. After all, I did this when I was a student. Now that I know a tiny bit more, I am updating the code. I have now used Smart Pointers and I have also updated the example App to show the Processor load in a Progress Bar. Furthermore, this is now a Visual Studio 2008 project.
Another fix was to add a lib file in linker settings.


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


About the Author

Aamir Butt
Technical Lead
Pakistan Pakistan
No Biography provided

Comments and Discussions

GeneralWeak Code PinmvpElmue2-Sep-10 17:45 
GeneralRe: Weak Code [modified] PinmemberBlake Miller22-Sep-10 12:26 
GeneralRe: Weak Code PinmvpElmue22-Sep-10 16:34 
GeneralError while compiling PinmemberBhaskartrivedi25-Aug-08 9:32 
GeneralRe: Error while compiling PinmemberPriya_Sundar1-Apr-09 1:59 

Basically they are defined in 'ObjIdl.Idl'.

Try to locate that in your system. It is a part of SDK.

Make sure one of those paths are included in you visual studio(Tools->options->Directories.)

I defined 'D:\Program Files\Microsoft SDK\include' into it and my errors got resolved.

Priya Sundar

GeneralMissing code.. PinmemberChizI23-May-08 6:08 
GeneralUnable to make remote Connection Pinmemberanushka.babu16-Apr-08 23:33 
Generalnot working under win 2k env Pinmemberbhanu_codeproject21-Nov-07 20:49 
GeneralRe: not working under win 2k env PinmemberUserMarcus24-Sep-08 4:02 
GeneralRe: not working under win 2k env PinmemberUserMarcus2-Oct-08 5:22 
GeneralIn the first attenpt itself it has worked Pinmemberbhanu_codeproject21-Nov-07 0:47 
QuestionHow to call WMI in MFC Externsion DLL Pinmemberyoungkwan song30-Aug-07 22:03 
GeneralAccessing IE settings in Vista PinmemberDana Daugherty11-Jun-07 23:10 
GeneralMissing DLL and header files PinmemberPeterLeblang22-Jan-07 2:54 
GeneralRe: Missing DLL and header files PinmemberAamir Butt22-Jan-07 19:26 
GeneralHelp me please to recognize situation by using WMI Pinmembermaria.net7-Aug-06 4:07 
QuestionI got the same error Pinmemberhoker.ffb4-Jul-06 0:34 
General#define _WIN32_DCOM PinmemberSpitfireX16-May-06 2:57 
GeneralRe: #define _WIN32_DCOM Pinmembertyjjrtruy4teqye7nyue7qay8-Jan-07 21:33 
GeneralRe: #define _WIN32_DCOM Pinmemberti-oh17-Jan-07 0:28 
GeneralIP Address Pinmemberdianalexander9-Feb-06 22:19 
GeneralRe: IP Address PinmemberAamir Butt12-Feb-06 18:24 
GeneralRe: IP Address Pinmembermcxiand15-May-06 21:58 
GeneralRe: IP Address PinmemberOrkblutt5-Oct-06 3:32 
Generalunhandled Win32 exception PinmemberIronic26623-Nov-05 22:27 
GeneralRe: unhandled Win32 exception PinmemberAamir Butt24-Nov-05 19:45 
GeneralRe: unhandled Win32 exception PinmemberIronic26626-Nov-05 23:32 
GeneralConnect to several namespaces Pinmemberangelzoo14-Nov-05 3:39 
GeneralRe: Connect to several namespaces PinmemberAamir Butt24-Nov-05 19:53 
GeneralRe: Connect to several namespaces Pinmemberangelzoo24-Nov-05 23:09 
QuestionMissing CoInitializeSecurity? Pinmembertikcireviva27-Sep-05 1:34 
AnswerRe: Missing CoInitializeSecurity? PinmemberAamir Butt24-Nov-05 19:55 
GeneralRe: Missing CoInitializeSecurity? Pinmemberkeithlee27-Feb-06 10:50 
AnswerRe: Missing CoInitializeSecurity? Pinmemberxiaozhijian11-Jan-06 21:44 
GeneralRe: Missing CoInitializeSecurity? PinmemberZenith6315-Jul-07 10:17 
GeneralRe: Missing CoInitializeSecurity? PinmemberMember 376488919-Jul-11 19:14 
Questionhow to connect romote host Pinmemberwesley424812-May-05 16:36 
AnswerRe: how to connect romote host PinmemberAamir Butt24-Nov-05 20:01 
Generalwbemuuid.lib - CoCreateInstance problem Pinmemberviswanath_jntu9-Mar-05 20:13 
GeneralRe: wbemuuid.lib - CoCreateInstance problem PinmemberPeter Beecken18-Nov-05 3:44 
GeneralRe: wbemuuid.lib - CoCreateInstance problem PinmemberJNygren11-Apr-07 6:37 
GeneralRe: wbemuuid.lib - CoCreateInstance problem Pinmemberpilixuanke8-May-07 4:09 
Generalwbemuuid.lib - error :object file corrupted Pinmemberviswanath_jntu9-Mar-05 19:06 
GeneralWin32_OperatingSystem Pinmember<>7-Mar-05 7:10 
GeneralRe: Win32_OperatingSystem Pinmembersmartceo6-Jun-07 21:05 
GeneralRe: Win32_OperatingSystem Pinmemberdon_francc15-Sep-10 20:32 
Generalwin2k error!!! Pinmemberiicastle30-Jan-05 23:07 
GeneralRe: win2k error!!! PinmemberAamir Butt30-Jan-05 23:14 
GeneralProblem under Win2K PinmemberAxelHecker3-Jan-05 5:38 
Questionwhere to get Wbemcli.h? Pinmemberalfredosp6-Oct-04 10:08 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.5 | Last Updated 27 Sep 2011
Article Copyright 2004 by Aamir Butt
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid