Use WMI (original English) in VC Author: BugSearcher

xiaoxiao2021-03-06  68

Introduction

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.

Code

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 imports:

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 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 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 Availab le. 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.coinitialize (null);

// Security Needs to Be Initialized in XP First

IF (CoinitializeSecurity (NULL,

-1,

NULL,

NULL,

RPC_C_AUTHN_LEVEL_PKT,

RPC_C_IMP_LEVEL_IMPERSONATE,

NULL,

EOAC_NONE,

0

)! = S_ok) return;

Iwbemlocator * piwbemlocator = null;

IWBEMSERVICES * PWBEMSERVICES = NULL;

IenumwbemclassObject * penumObject = null;

BSTR BSTRNAMESPACE = (l "root // CIMv2");

HRESULT HRES = CoCreateInstance

CLSID_WBEMADMINISTRATIVELOCATOR,

NULL,

CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,

IID_IUNKNOWN,

(void **) & piwbemlocator

);

"succeeded (hres))

{

HRES = PIWBEMLOCATOR-> ConnectServer

BStrNamespace, // Namespace

Null, // userid

Null, // PW

Null, // locale

0, // Flags

Null, // authority

Null, // context

& pwbemservices

);

}

BSTR strquery = (l "Select * from win32_processor);

BSTR strql = (l "wql");

HRES = PWBEMSERVICES-> EXECQUERY (STRQL, STRQUERY,

WBEM_FLAG_RETURN_IMMEDIATELY, NULL, & PENUMOBJECT);

Ulong ucount = 1, ureturned;

IwbemclassObject * pclassObject = null;

HRES = PenumObject-> reset ();

HRES = PenumObject-> Next (WBEM_INFINITE, UCOUNT, & PCLASSOBJECT, & URETURNED);

Variant V;

BSTR strclassprop = sysallocstring (l "loadpercentage");

HRES = PCLASSOBJECT-> GET (Strclassprop, 0, & V, 0, 0);

SysfreeString (STRCLASSPROP);

_BSTR_T BSTRPATH = & V; // Just to Convert Bstr To Ansi

Char * strpath = (char *) BSTRPATH;

"succeeded (hres))

MessageBox (STRPATH);

Else

MessageBox ("Error In Getting Object");

Variantclear (& V);

PIWBEMLOCATOR-> Release ();

PWBemServices-> release ();

PenumObject-> Release ();

PclassObject-> Release ();

Couninitialize ();

Conclusion

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 Tested This Code in Windows 2000 Professional. I Hope It Work Work Well for Win XP But Probably Not in Previous Versions of Windows Because They Don't support a lot of wmi classes.Update

I am updating this Article and the code for Windows XP. As I said in the beginning, this was initially intended for Windows 2K and it was still working fine on Windows 2K machine (I have checked it on at least 10 now coz I was getting so many complaints). Well, for Windows XP there is one major change and that was suggested by igoychev. Many thanks for that. The problem with Windows XP was that it needed security to be initialized first. Plus, I have also changed those confusing . slashes in bstrNamespace to some simpler ones I am using Windows 2003 Advance Server and it is working fine on it You need to include wbemuuid.lib in Project Settings -.> Linker -.> Additional Dependencies to get this code working I have also added some error checking to avoid crashes, hope it works for you. you need to have Platform SDK installed for this code to compile. WMI SDK is not necessary once again. Sorry rbervgm, your work was great using WMI SDK but I thought better to keep IT WMI SDK Clean. Hope Y Ou Don't Mind. Now, I Hope That Downloadable Project Will Work for MOST OF THE PEOPLE. ONE More Thing, Its Still Works for Windows 2k Machines.

转载请注明原文地址:https://www.9cbs.com/read-120303.html

New Post(0)