The "WMI-4" WMI for C++/com call method __c++

Source: Internet
Author: User
WIN32 Provider not only provides information about classes and instances, but some provider provide methods that users can invoke. The process of invoking the method is as follows:
1) Get provider instance Object
Call the Iwbemservices::getobject method to get the provider type instance that we want to call, and he returns with a IWbemClassObject type pointer.
BSTR methodname = SysAllocString (L "Create");
BSTR ClassName = SysAllocString (L "Win32_Process");
IWbemClassObject * Pclass = NULL;
hres = Psvc-> GetObject (ClassName, 0, NULL, & Pclass, NULL); 2) Obtain provider provide method parameters
Call the Iwbemclassobject::getmethod method to get the arguments we want to call the method, and he returns with a IWbemClassObject type pointer.
IWbemClassObject * pinparamsdefinition = NULL;
hres = Pclass-> getmethod (methodname, 0,
& Pinparamsdefinition, NULL); 3 generates an object that provider the parameters of the method supplied
Call the Iwbemclassobject::spawninstance method to generate a parameter instance of the calling method. You need to call the SpawnInstance method with the parameter type pointer obtained in the second step, and pass the method a iwbemclassobject pointer as a pointer to the generated parameter object.
IWbemClassObject * pclassinstance = NULL;
hres = pinparamsdefinition-> spawninstance (0, & Pclassinstance); 4 Set the properties of the Parameter object
Call IWbemClassObject::P ut method to set the type of the Parameter object.
VARIANT Varcommand;
VARCOMMAND.VT = VT_BSTR;
Varcommand.bstrval = L "notepad.exe";
hres = pclassinstance-> put (L "CommandLine", 0,
& Varcommand, 0);
wprintf (L "The command is:%s", V_bstr (& Varcommand)); 5) Calling method
The same as the query information, the call method can be divided into synchronous and asynchronous way, synchronous mode waiting for the execution process to end, continue to execute; Asynchronous methods use the type that implements the IWbemObjectSink interface, create a new thread, and then continue running the current thread, and the new thread completes the calling method , and then callback IWbemObjectSink:: Indicate function, handle function return value.
The following is a method called provider provided by calling the Iwbemservices::execmethod method synchronously, passing the previously generated instance of the parameter to the method.
IWbemClassObject * Poutparams = NULL;
hres = Psvc-> execmethod (ClassName, methodname, 0,
NULL, Pclassinstance, & Poutparams, NULL); The following are functions that invoke the same provider asynchronously, unlike synchronous methods, which call the Iwbemservices::execmethodasync method asynchronously, and the third parameter Iflag can be set to Wbem_flag_send_ Status to accept the intermediate state information at the time of the call, there is no output parameter (Poutparams in this case), and the last parameter is the type that implements the IWbemObjectSink interface.
Querysink * Psink = new Querysink ();
hres = Psvc-> execmethodasync (ClassName, MethodName, Wbem_flag_send_status,
NULL, Pclassinstance, Psink); However, the invocation method is a half synchronous method than the query information, because the synchronous methods make the caller thread wait for the method to finish, while the asynchronous method requires the programmer to inherit the IWbemObjectSink interface and do multithreaded programming. So the IWbemServices is a compromise method for the invocation of the method in the instance, create a thread to query, and then write to a Iwbemcallresult interface instance, which is queried by the main thread.
Iwbemcallresult * pcallres = 0;
IWbemClassObject * pobj = 0;
hres = Psvc-> execmethod (ClassName, MethodName, wbem_flag_return_immediately,
NULL, Pclassinstance,null, & Pcallres);

while (true)
... {
LONG lstatus = 0;
HRESULT hres = Pcallres->getcallstatus (5000, &lstatus);
if ((hres = = Wbem_s_no_error))
Break
if ((hres = = wbem_s_timedout))
Continue
}

hres = pcallres-> getresultobject (5000, & Pobj);
if (hres)
... {
Pcallres->release ();
return 1;
}

VARIANT Varreturnvalue;
hres = Pobj-> Get (_bstr_t (L "returnvalue"), 0,
& Varreturnvalue, NULL, 0);

cout << varreturnvalue.lval << Endl;
Pcallres-> release ();
Pobj-> release ();
The above is the basic form of invoking a method via WMI, where there is less functionality to invoke the method under XP, but in Vista, Windows provides some provider and methods in the Root/wmi namespace instead of something that was originally driven.
Unfortunately, some of the documentation on MSDN is inconsistent with the data actually embodied by the WMI tool provided by MS, as with the two parameters of the Setbrightness method of the Wmisetbrightnessmethods class. Uint8 and UInt64 seems to be prepared for the Dotnet platform as well, in put in these two parameters, hres return S_OK, but in the ExecMethod, there will be type mismatch error, let a person more scratching head ~ Hope if a friend passing by and happen to know how to use c+ +com call this method can give me a message, or add my QQ, thank you very much.

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.