Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Invoking a Synchronous Query

A synchronous query is a query that maintains control over the process of your application for the duration of the query. A synchronous query requires a single interface call, and is therefore more simple than an asynchronous call. However, a synchronous query has the potential of locking up your application for large queries or queries over a network.

The following code example shows a synchronous query for data. It only queries for Win32_Service objects where Win32_Service.State property value is "Stopped".


for each Service in _ 
    GetObject("winmgmts:").ExecQuery _
        ("Select * from Win32_Service where State='Stopped'")
    WScript.Echo "  "& Service.DisplayName & " [", Service.Name, "]"
next

The following procedure describes how to issue a synchronous data query using VBScript.

Aa391398.wedge(en-us,VS.85).gifTo issue a synchronous data query in VBScript

  1. Describe your query to WMI using SWbemServices.ExecQuery. This method returns an SWbemObjectSet.
    
    GetObject("winmgmts:").ExecQuery _
            ("Select * from Win32_Service where State='Stopped'")
    
    
  2. Use standard scripting language collection techniques to access each returned object.
    
    for each Service in _ 
        GetObject("winmgmts:").ExecQuery _
            ("Select * from Win32_Service where State='Stopped'")
        WScript.Echo "  "& Service.DisplayName & " [", Service.Name, "]"
    next
    
    

The following procedure describes how to issue a synchronous data query using C++.

Aa391398.wedge(en-us,VS.85).gifTo issue a synchronous query in C++

  1. Describe your query to WMI through a call to IWbemServices::ExecQuery.

    The ExecQuery method takes a WQL search string as a parameter that describes your query. WMI performs the query and returns an IEnumWbemClassObject interface pointer. Through the IEnumWbemClassObject interface, you can access the classes or instances that make up the result set.

  2. After you receive your query, you can enumerate your query with a call to IEnumWbemClassObject::Next. For more information, see Enumerating WMI.

    The following code example requires the following references and #include statements to compile correctly.

    
    #include <wbemidl.h>
    #include <iostream>
    using namespace std;
    
    

    The following code example describes how to query for the objects that represent the users and groups in WMI.

    
    void ExecQuerySync(IWbemServices *pSvc)
    {
        // Query for all users and groups.
    
        BSTR Language = SysAllocString(L"WQL");
        BSTR Query = SysAllocString(L"SELECT * FROM __Namespace");
    
        // Initialize the IEnumWbemClassObject pointer.
        IEnumWbemClassObject *pEnum = 0;
    
        // Issue the query.
        HRESULT hRes = pSvc->ExecQuery(
            Language,
            Query,
            WBEM_FLAG_FORWARD_ONLY,         // Flags
            0,                              // Context
            &pEnum
            );
    
        SysFreeString(Query);
        SysFreeString(Language);
    
        if (hRes != 0)
        {
            printf("Error\n");
            return;
        }
        
        ULONG uTotal = 0;
    
        // Retrieve the objects in the result set.
        for (;;)
        {
            IWbemClassObject *pObj = 0;
            ULONG uReturned = 0;
    
            hRes = pEnum->Next(
                0,                  // Time out
                1,                  // One object
                &pObj,
                &uReturned
                );
    
            uTotal += uReturned;
    
            if (uReturned == 0)
                break;
    
            // Use the object.
            
            // ...
            
            // Release it.
            // ===========
            
            pObj->Release();    // Release objects not owned.            
        }
    
        // All done.
        pEnum->Release();
    }
    
    

 

 

Show:
© 2015 Microsoft