How VC ++ and. Net change from WMI

Source: Internet
Author: User
WMI (Windows Management Instrumentation) is a management framework that can be used with system information (including software and hardware) in windows. WMI can be used to conveniently manage machines. Now let's take a look at the changes from VC ++ to. Net by taking the wmito bypass or create a notebook (notepad.exe) process as an example. Let's take a look at how. Net frees programmers from tedious programs.

1. Pre-work:
In VC ++, you need to add the following to the source code:
# Include <wbemidl. h>
# Pragma comment (Lib, "wbemuuid. lib ")

VC # requires:
Add reference in Project: system. Management
Add using system. Management to the Code;

2. Process:
The VC ++ code requires 6 steps and query return values, and finally releases the resource: // Step 1 :--------------------------------------------------
// Initialize com .------------------------------------------
Hres = coinitializeex (0, coinit_multithreaded );

// Step 2 :--------------------------------------------------
// Set general com security levels --------------------------
Hres = coinitializesecurity (
Null,
-1, // com negotiates Service
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
);

// Step 3 :---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
Iwbemlocator * ploc = NULL;
Hres = cocreateinstance (
Clsid_wbemlocator,
0,
Clsctx_inproc_server,
Iid_iwbemlocator, (lpvoid *) & ploc );

// Step 4 :---------------------------------------------------
// Connect to WMI through the iwbemlocator: connectserver Method
Iwbemservices * psvc = NULL;
// Connect to the Local Root \ cimv2 namespace
// And obtain pointer psvc to make iwbemservices CILS.
Hres = ploc-> connectserver (
_ Bstr_t (L "Root \ cimv2 "),
Null,
Null,
0,
Null,
0,
0,
& Psvc
);

// Step 5 :--------------------------------------------------
// Set security levels for the proxy ------------------------
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
);

// Step 6 :--------------------------------------------------
// Use the iwbemservices pointer to make requests of WMI ----
// Set up to call the win32_process: Create method
BSTR methodname = sysallocstring (L "CREATE ");
BSTR classname = sysallocstring (L "win32_process ");

Iwbemclassobject * Pclass = NULL;
Hres = psvc-> GetObject (classname, 0, null, & Pclass, null );

Iwbemclassobject * pinparamsdefinition = NULL;
Hres = Pclass-> getmethod (methodname, 0,
& Pinparamsdefinition, null );

Iwbemclassobject * pclassinstance = NULL;
Hres = pinparamsdefinition-> spawninstance (0, & pclassinstance );

// Create the values for the in Parameters
Variant varcommand;
Varcommand. Vt = vt_bstr;
Varcommand. bstrval = l "notepad.exe ";
// Store the value for the in Parameters
Hres = pclassinstance-> put (L "CommandLine", 0,
& Varcommand, 0 );
Wprintf (L "the command is: % s \ n", v_bstr (& varcommand ));

// Execute Method
Iwbemclassobject * poutparams = NULL;
Hres = psvc-> execmethod (classname, methodname, 0, null, pclassinstance, & poutparams, null );

// Get return value
Variant varreturnvalue;
Hres = poutparams-> get (_ bstr_t (L "returnvalue"), 0, & varreturnvalue, null, 0 );

// Last: Clean Up
Variantclear (& varcommand );
Variantclear (& varreturnvalue );
Sysfreestring (classname );
Sysfreestring (methodname );
Pclass-> release ();
Pinparamsdefinition-> release ();
Poutparams-> release ();
Ploc-> release ();
Psvc-> release ();
Couninitialize ();

VC # only a few lines can be implemented (ignore error handling for the moment ):
Skip Step 1 ~ in VC ++ ~ 5. Start with Step 6. And very intuitive: managementclass MC = new managementclass ("win32_process ");
Managementbaseobject OBJ = mc. getmethodparameters ("CREATE ");
OBJ ["CommandLine"] = "notepad.exe ";
MC. invokemethod ("CREATE", OBJ, null );
MC. Dispose ();

. NET is well-encapsulated for WMI, and has a stronger support for strings and a convenient garbage collection mechanism, making the program both clear and easy to maintain. In fact, similar differences have emerged in the Age of VC vs VB (especially in the compilation and calling of COM components. net completely inherits the features of easy to learn and use of VB, but also powerful functions.

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.