"WMI-3" WMI for c++/com query information __c++

Source: Internet
Author: User
Query information
One of the main functions of WMI is to query the WMI repository for information about classes and instances, we can call a synchronous query, or we can call an asynchronous query.
A synchronous query is a query that executes at the current execution, and an asynchronous query needs to start another thread to query, and after the query has finished, call the defined interface (similar to a callback function) to handle the query results.
Synchronous query is suitable for querying native system or some other small application; When the workload of the query is very large, the synchronization query may cause the waiting of resources, so the asynchronous query is a better choice for the query and a large number of queries applied to the network.
1) Synchronous Query
The COM type required for a synchronous query is IWbemServices, and the method is ExecQuery, and what we need to do is pass an edited WQL query string as a parameter to the method, which returns a pointer to the Ienumwbemclassobject type. The type of pointer holds the result set of our query. With this result set, we can call the Ienumwbemclassobject::next function to traverse the result set and then select the data we need exactly.
Ienumwbemclassobject * Penumerator = NULL;
hres = Psvc-> ExecQuery (
bstr_t ("WQL"),
bstr_t ("SELECT * from Win32_BIOS"),
wbem_flag_forward_only | Wbem_flag_return_immediately,
Null
& Penumerator);

if (FAILED (hres))
... {
cout << "Query for operating system name failed."
<< "Error code = 0x"
<< hex << hres << Endl;
Psvc->release ();
Ploc->release ();
CoUninitialize ();
return 1; Program has failed.
}

Traverse
IWbemClassObject * PCLSOBJ;
ULONG Ureturn = 0;
while (Penumerator)
... {
HRESULT hr = Penumerator->next (wbem_infinite, 1,
&pclsobj, &ureturn);

if (0 = ureturn)
... {
Break
}

VARIANT Vtprop;
VariantInit (&vtprop);

Get BIOS serial Number
hr = Pclsobj->get (L "SerialNumber", 0, &vtprop, 0, 0);
Wcout << "BIOS serialnumber:" << vtprop.bstrval << Endl;
Get BIOS version
hr = Pclsobj->get (L "Version", 0, &vtprop, 0, 0);
Wcout << "BIOS Version:" << vtprop.bstrval << Endl;

VariantClear (&vtprop);
}
2) Asynchronous query
COM type IWbemServices is also required for asynchronous queries, ExecQueryAsync. This method moves to another thread from the process currently querying the WMI repository, making another query work. WMI invokes the indicate method of the type that implements the Iwbemsinkobject interface to notify the program that the query ends and, like a synchronous query, returns the result set of the query to the user as a pointer to a ienumwbemclassobject type.
The following code shows only the methods of the asynchronous query, but the method in response to the query results is found in MSDN in the custom class Querysink, due to space limitations.
Create a new sink object.
Querysink * Psink = new Querysink;

Initialize the query and query language.
BSTR strquery = SysAllocString (L "select * from ClassName");
BSTR Strquerylang = SysAllocString (L "WQL");

Issue the query.
HRESULT hres = psvc-> execqueryasync (Strquerylang, strquery, 0,
NULL, Psink);

Clean up.
SysFreeString (strquery);
SysFreeString (Strquerylang);

if (hres)
... {
printf ("ExecQueryAsync failed with = 0x%X", hres);
Return

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.