One of WMI programming: Using WMI in VC

Source: Internet
Author: User
One of WMI programming: Using WMI in VC

After a week of busy work, I finally had some time to write a blog. Now I will talk about How to Use WMI in VC. Next we will build a simple WMI application framework in five steps.

Do not forget to include header files and linked library files before writing programs.

# Include <wbemidl. h>

# Pragma comment (Lib,
"Wbemuuid. lib ")

Then we can.

Step 1: Initialize com As mentioned earlier, WMI is based on COM (Component Object Model), so before Using WMI, we must first initialize COM. Two functions are used here.

Hresult hres;

// Initialize COM.

Hres = coinitializeex (0, coinit_multithreaded );

If (failed (hres ))

{

Cout <"failed to initialize com library ."

<"Error code = 0x"

<Hex

Return 1;
// Program has failed.

}

 

// Set the process Security Level

Hres = coinitializesecurity (

Null,

-1, // com negotiates Service

Null, // authentication services

Null, // Reserved

Rpc_c_authn_level_default, // Authentication

Rpc_c_imp_level_impersonate, // impersonation

Null, // authentication info

Eoac_none, // additional capabilities

Null // Reserved

);

 

If (failed (hres ))

{

Cout <"failed to initialize security ."

<"Error code = 0x"

<Hex

Couninitialize ();

Return 1;
// Program has failed.

}

At this point, even the initialization of COM is complete. Next, go to the next step.

Step 2: Create a WMI namespace connection.

WMI uses a uniform namespace.

// Create a clsid_wbemlocator object

Iwbemlocator * ploc = 0;

Hres = cocreateinstance (

Clsid_wbemlocator,

0,

Clsctx_inproc_server,

Iid_iwbemlocator, (lpvoid *) & ploc );

 

If (failed (hres ))

{

Cout <"failed to create iwbemlocator object ."

<"Error code = 0x"

<Hex

Couninitialize ();

Return 1;
// Program has failed.

}

 

Iwbemservices * psvc = 0;

// Use ploc to connect to "Root \ cimv2" and get the pointer of psvc.

Hres = ploc-> connectserver (

_ Bstr_t (L "Root \ cimv2 "),
// WMI namespace

Null, // User Name

Null, // User Password

0, // locale

Null, // Security flags

0, // authority

0, // context object

& Psvc // iwbemservices proxy

);

If (failed (hres ))

{

Cout <"cocould not connect. Error code = 0x"

<Hex

Ploc-> release ();

Couninitialize ();

Return 1;
// Program has failed.

}

// You have connected to WMI.

Cout <"connected to Root \ cimv2 WMI namespace" <Endl;

Step 3: Set the connection security level.

Why? I don't know, Ms said it would be like this :)

Hres = cosetproxyblanket (

Psvc, // The proxy to set

Rpc_c_authn_winnt, // Authentication Service

Rpc_c_authz_none, // Authorization Service

Null, // server principal name

Rpc_c_authn_level_call, // Authentication Level

Rpc_c_imp_level_impersonate, // impersonation level

Null, // client identity

Eoac_none // proxy capabilities

);

 

If (failed (hres ))

{

Cout <"cocould not set proxy blanket. Error code = 0x"

<Hex

Psvc-> release ();

Ploc-> release ();

Couninitialize ();

Return 1;
// Program has failed.

}

Step 4: Execute your code to achieve your goal.

The concept of WQL should be introduced accidentally here.

WQL is the query language in WMI. The full name of WQL is WMI query language (WQL for short). It can be translated into Chinese and become a standardized query language for Windows Management. Those who are familiar with the SQL language will feel that it is very similar to SQL.

 

WQL is actually very simple and has the following features:

1. Each WQL statement must start with select;

2. Select is followed by the attribute name you want to query (I called it the field name for the corresponding SQL). You can also return all attribute values in the same way as SQL;

3. From keyword;

4. Name of the class to be queried;

 

// Here is an example of listing Running Processes

// To receive results, you must define an enumeration object

Ienumwbemclassobject * penumerator = NULL;

Hres = psvc-> execquery (

Bstr_t ("WQL "),

Bstr_t ("select * From win32_process "),

Wbem_flag_forward_only | wbem_flag_return_immediately,

Null,

& Penumerator );

If (failed (hres ))

{

Cout <"query for processes failed ."

<"Error code = 0x"

<Hex

Psvc-> release ();

Ploc-> release ();

Couninitialize ();

Return 1;
// Program has failed.

}

Else

{

Iwbemclassobject * pclsobj;

Ulong ureturn = 0;

While (penumerator)

{

// Launch the next object

Res = penumerator-> next (wbem_infinite, 1,

& Pclsobj, & ureturn );

// Jump out if there is nothing left

If (0 = ureturn)

{

Break;

}

 

Variant vtprop;

 

// Get the value of the Name property

Hres = pclsobj-> get (L "name", 0, & vtprop, 0, 0 );

Wcout <"process name:" <vtprop. bstrval <Endl;

Variantclear (& vtprop );

}

}

Step 5: clear and close your program.

Releasing this release is a good habit.

Psvc-> release ();

Ploc-> release ();

Couninitialize ();

At this point, we have basically completed the simplest WMI application framework. Do you still have a lot of work to do? It doesn't matter. Next time we will introduce a framework with a UI for your convenience.

Here is an example of this chapter: http://files.cnblogs.com/hamwolf/WMIBase.zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.