Win32_operatingsystem for WMI Programming

Source: Internet
Author: User

# Include <iostream>
# Include <comdef. h>
# Include <wbemidl. h>

# Include <atlbase. h>
# Include <atlstr. h>

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

Create a common console project and Add ATL support

//
// Initialize com
//

Hresult hres;

Hres = coinitializeex (0, coinit_multithreaded );
If (failed (hres ))
{
Return 1; // program has failed.
}

Hres = coinitializesecurity (null,
-1, // com Authentication
Null, // authentication services
Null, // Reserved
Rpc_c_authn_level_default, // Default Authentication
Rpc_c_imp_level_impersonate, // default 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.
}

//
// Cocreateinstance creates a locator instance.
//

Ccomqiptr <iwbemlocator> ploc;

Hres = cocreateinstance (
Clsid_wbemlocator,
0,
Clsctx_inproc_server,
Iid_iwbemlocator, (lpvoid *) & ploc );

If (failed (hres ))
{
Couninitialize ();
Return 1; // program has failed.
}

//
// Connect to the "root/cimv2" namespace to obtain the iwbemservices pointer.
//

Ccomqiptr <iwbemservices> psvc;

Hres = ploc-> connectserver (
_ Bstr_t (L "root // cimv2"), // Object Path of WMI namespace
Null, // user name. null = current user
Null, // user password. null = Current
0, // locale. null indicates current
Null, // Security flags.
0, // Authority (e.g. Kerberos)
0, // context object
& Psvc // pointer to iwbemservices proxy
);

If (failed (hres ))
{
Couninitialize ();
Return 1; // program has failed.
}

//
// Sets the iwbemservices proxy security, so that the WMI Service is equivalent to the client
//
Hres = cosetproxyblanket (
Psvc, // indicates the proxy to set
Rpc_c_authn_winnt, // rpc_c_authn_xxx
Rpc_c_authz_none, // rpc_c_authz_xxx
Null, // server principal name
Rpc_c_authn_level_call, // rpc_c_authn_level_xxx
Rpc_c_imp_level_impersonate, // rpc_c_imp_level_xxx
Null, // client identity
Eoac_none // proxy capabilities
);

If (failed (hres ))
{
Couninitialize ();
Return 1;
}

//
// Use the iwbemservices pointer to request WMI to obtain information about the operating system
//
Ccomqiptr <ienumwbemclassobject> penumerator;

Hres = psvc-> execquery (
Bstr_t ("WQL "),
Bstr_t ("select * From win32_operatingsystem "),
Wbem_flag_forward_only | wbem_flag_return_immediately,
Null,
& Penumerator );

If (failed (hres ))
{
Couninitialize ();
Return 1; // program has failed.
}

//
// Display WQL query data
//
 
Ulong ureturn = 0;

While (penumerator)
{

Ccomqiptr <iwbemclassobject> pclsobj;
Hresult hR = penumerator-> next (wbem_infinite, 1, & pclsobj, & ureturn );
If (0 = ureturn)
{
Break;
}

_ Variant_t vtprop;


// Obtain the operating system name
HR = pclsobj-> get (L "name", 0, & vtprop, 0, 0 );
// Obtain the system startup Partition
Pclsobj-> get (L "bootdevice", 0, & vtprop, 0, 0 );
// Obtain the number of processes currently running
Pclsobj-> get (L "numberofprocesses", 0, & vtprop, 0, 0 );
// Obtain the serial number of the Operating System
Pclsobj-> get (L "serialnumber", 0, & vtprop, 0, 0 );
 // Obtain the language of the Operating System
Pclsobj-> get (L "oslanguage", 0, & vtprop, 0, 0 );
//
// This attribute cannot be set because it is read-only.
// Vtprop. lval = 0x0809;
// Pclsobj-> put (L "oslanguage", 0/* Reserved */, & vtprop, vt_i4 );
//
// Obtain the manufacturer name
//
Pclsobj-> get (L "manufacturer", 0, & vtprop, 0, 0 );
}

Couninitialize ();
 

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.