ExecQueryfunction
Executes a query to retrieve objects.
Syntax
FUNCTION ExecQuery (BYREF wszQuery AS WSTRING, BYVAL iFlags AS LONG = wbemFlagReturnWhenComplete) AS HRESULT
Parameters
| Name | Description | |
|---|---|---|
wszQuery | Required. A string that contains the text of the query. This parameter cannot be blank. | |
iFlags | Optional. An integer that determines the behavior of the query and determines whether this call returns immediately. The default value for this parameter is wbemFlagReturnWhenComplete. This parameter can accept the following values. |
Return value
Returns S_OK (0) on success, or an HRESULT code on failure.
Description
Executes a query to retrieve objects. These objects are available through the retrieved SWbemObjectSet collection.
Examples
#include "AfxNova/CWmiDisp.inc" USING AfxNova
' // Connect to WMI using a moniker ' // Note: $ is used to avoid the pedantic warning of the compiler about escape characters DIM pServices AS CWmiServices = $"winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2"
' // Execute a query DIM hr AS HRESULT = pServices.ExecQuery("SELECT Caption, SerialNumber FROM Win32_BIOS")
' // Get the number of objects retrieved DIM nCount AS LONG = pServices.ObjectsCount print "Count: ", nCount
' // Enumerate the objects using the standard IEnumVARIANT enumerator (NextObject method) ' // and retrieve the properties using the CDispInvoke class. DIM pDispServ AS CDispInvoke = pServices.NextObject PRINT "Caption: "; pDispServ.Get("Caption") PRINT "Serial number: "; pDispServ.Get("SerialNumber")
#include "AfxNova/CWmiDisp.inc" USING AfxNova
' // Connect to WMI using a moniker ' // Note: $ is used to avoid the pedantic warning of the compiler about escape characters DIM pServices AS CWmiServices = $"winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2"
' // Execute a query DIM hr AS HRESULT = pServices.ExecQuery("SELECT * FROM Win32_Printer")
' // Get the number of objects retrieved DIM nCount AS LONG = pServices.ObjectsCount
' // Enumerate the objects FOR i AS LONG = 0 TO nCount - 1 PRINT "--- Index " & STR(i) & " ---" DIM pDispServ AS CDispInvoke = pServices.NextObject PRINT "Caption: "; pDispServ.Get("Caption") PRINT "Capabilities "; pDispServ.Get("Capabilities") NEXT
#include "AfxNova/CWmiDisp.inc" USING AfxNova
' // Connect to WMI using a moniker ' // Note: $ is used to avoid the pedantic warning of the compiler about escape characters DIM pServices AS CWmiServices = $"winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2"
' // Execute a query DIM hr AS HRESULT = pServices.ExecQuery("SELECT Caption, SerialNumber FROM Win32_BIOS", 48)
' // Enumerate the objects using the standard IEnumVARIANT enumerator (NextObject method) ' // and retrieve the properties using the CDispInvoke class. DIM pDispServ AS CDispInvoke = pServices.NextObject PRINT "Caption: "; pDispServ.Get("Caption") PRINT "Serial number: "; pDispServ.Get("SerialNumber")
#include "AfxNova/CWmiDisp.inc" USING AfxNova
' // Connect to WMI using a moniker ' // Note: $ is used to avoid the pedantic warning of the compiler about escape characters DIM pServices AS CWmiServices = $"winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2"
' // Execute a query DIM hr AS HRESULT = pServices.ExecQuery("SELECT * FROM Win32_Printer", 48)
' // Enumerate the objects using the standard IEnumVARIANT enumerator (NextObject method) ' // and retrieve the properties using the CDispInvoke class. DIM pDispServ AS CDispInvoke DO pDispServ = pServices.NextObject IF pDispServ.DispPtr = NULL THEN EXIT DO PRINT "Caption: "; pDispServ.Get("Caption") PRINT "Capabilities "; pDispServ.Get("Capabilities") LOOP
#include "AfxNova/CWmiDisp.inc" USING AfxNova
' // Connect to WMI using a moniker ' // Note: $ is used to avoid the pedantic warning of the compiler about escape characters DIM pServices AS CWmiServices = $"winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2"
' // Execute a query DIM hr AS HRESULT = pServices.ExecQuery("SELECT Caption, SerialNumber FROM Win32_BIOS")
' // Get the number of objects retrieved DIM nCount AS LONG = pServices.ObjectsCount
' // Get a collection of named properties IF pServices.GetNamedProperties <> S_OK THEN PRINT "Failed to get the named properties"
' // Retrieve the value of the properties PRINT pServices.PropValue("Caption") PRINT pServices.PropValue("SerialNumber")
#include "AfxNova/CWmiDisp.inc" USING AfxNova
' // Connect to WMI using a moniker ' // Note: $ is used to avoid the pedantic warning of the compiler about escape characters DIM pServices AS CWmiServices = $"winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2"
' // Execute a query DIM hr AS HRESULT = pServices.ExecQuery("SELECT * FROM Win32_Printer")
' // Get the number of objects retrieved DIM nCount AS LONG = pServices.ObjectsCount
' // Enumerate the objects FOR i AS LONG = 0 TO nCount - 1 PRINT "--- Index " & STR(i) & " ---" ' // Get a collection of named properties IF pServices.GetNamedProperties(i) = S_OK THEN
PRINT pServices.PropValue("Caption")
PRINT pServices.PropValue("Capabilities")
END IF NEXT
DO ' // fake loop to avoid nested IFs/ENDIFs
' // Connect to WMI using a moniker ' // Note: $ is used to avoid the pedantic warning of the compiler about escape characters DIM pServices AS CWmiServices = $"winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2" IF pServices.ServicesPtr = NULL THEN EXIT DO
' // Execute a query DIM hr AS HRESULT = pServices.ExecQuery("SELECT * FROM Win32_Process") IF hr <> S_OK THEN PRINT AfxWmiGetErrorCodeText(hr) : EXIT DO
' // Get the number of objects retrieved DIM nCount AS LONG = pServices.ObjectsCount print "Number of objects: ", nCount IF nCount = 0 THEN PRINT "No objects found" : EXIT DO
' // Enumerate the objects FOR i AS LONG = 0 TO nCount - 1
PRINT "--- Index " & STR(i) & " ---"
' // Get a collection of named properties
IF pServices.GetNamedProperties(i) = S_OK THEN
PRINT pServices.PropValue("Name")
PRINT pServices.PropValue("ProcessID")
PRINT pServices.PropValue("ThreadCount")
PRINT pServices.PropValue("PageFileUsage")
PRINT pServices.PropValue("PageFaults")
PRINT pServices.PropValue("WorkingSetSize")
END IF
NEXT
EXIT DO ' // Inconditional exit LOOP
DO ' // fake loop to avoid nested IFs/ENDIFs
' // Connect to WMI using a moniker ' // Note: $ is used to avoid the pedantic warning of the compiler about escape characters DIM pServices AS CWmiServices = $"winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2" IF pServices.ServicesPtr = NULL THEN EXIT DO
' // Execute a query DIM hr AS HRESULT = pServices.ExecQuery("SELECT * FROM Win32_Process", 48) IF hr <> S_OK THEN PRINT AfxWmiGetErrorCodeText(hr) : EXIT DO
' // Enumerate the objects using the standard IEnumVARIANT enumerator (NextObject method) ' // and retrieve the properties using the CDispInvoke class. DIM pDispServ AS CDispInvoke DO
pDispServ = pServices.NextObject
IF pDispServ.DispPtr = NULL THEN EXIT DO
PRINT "Name: "; pDispServ.Get("Name")
PRINT "ProcessID "; pDispServ.Get("ProcessID")
PRINT "ThreadCount "; pDispServ.Get("ThreadCount")
PRINT "PageFileUsage "; pDispServ.Get("PageFileUsage")
PRINT "PageFaults "; pDispServ.Get("PageFaults")
PRINT "WorkingSetSize "; pDispServ.Get("WorkingSetSize")
LOOP
EXIT DO ' // Inconditional exit LOOP
Using an enumerator (the standard IEnumVARIANT interface) to retrieve the information:
If the query returns more than one object, then we will use a loop:
To improve enumeration performance set the iFlags parameter of the ExecQuery method to WbemFlagReturnImmediately and WbemFlagForwardOnly (the combined value of these flags is 48) to allow semisynchronous return of the data with an enumerator that discards each item from WMI as it is delivered. In this case don't call the ObjectsCount method because it will return 0, since the operation has not been completed.
If there are several objects in the collection, we can use a loop:
Calling the GetNamedProperties method after executing the query. GetNamedProperties generates a named collection of properties. This has the advantage of not having to use CDispInvoke.
Using a loop:
The following example monitors process performance information.
Using an enumerator:
Reference
- Include file
CWmiDisp.inc - Documented in COM/CWmiDisp Class.md
- Topic: Windows Management Instrumentation (WMI)