Share via


Querying for Group Policy WMI Objects

After you retrieve an IWbemServices interface pointer, you can call the IWbemServices::ExecQuery method to search for objects of a particular class.

The following code example searches for all objects of the RSOP_RegistryPolicySetting class. RSOP_RegistryPolicySetting is a generic class that represents a registry key and a value, and the associated data. The sample retrieves the key name and value name for each object and prints the values to the console window.

    //
    // Allocate BSTRs for the WMI query language and for the query itself.
    //
    pLanguage = SysAllocString (TEXT("WQL"));
    pQuery = SysAllocString (TEXT("SELECT * FROM RSOP_RegistryPolicySetting"));
    //
    // Allocate BSTRs for the property names to retrieve.
    //
    pRegistryKey = SysAllocString (TEXT("registryKey"));
    pValueName = SysAllocString (TEXT("valueName"));
    //
    // Verify that the allocations succeeded.
    //
    if (pLanguage && pQuery && pRegistryKey && pValueName)
    {
        //
        // Execute the query.
        //
        hr = pIWbemServices->ExecQuery (pLanguage, pQuery, WBEM_FLAG_FORWARD_ONLY | 
                                WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnum);

        if (SUCCEEDED(hr))
        {
            //
            // Loop through the results; 
            //   retrieve the registry key and value names.
            //
            while (pEnum->Next(WBEM_INFINITE, 1, pObjects, &ulRet) == WBEM_S_NO_ERROR)
            {
                hr = pObjects[0]->Get (pRegistryKey, 0, &varRegistryKey, NULL, NULL);

                if (SUCCEEDED(hr))
                {
                    hr = pObjects[0]->Get (pValueName, 0, &varValueName, NULL, NULL);

                    if (SUCCEEDED(hr))
                    {
                        //
                        // Print the key and value names.
                        //
                        wprintf (L" %s\\%s\n", varRegistryKey.bstrVal, varValueName.bstrVal);
                        VariantClear (&varValueName);
                    }

                    VariantClear (&varRegistryKey);
                }

                ulCount++;
            }

            if (ulCount == 0)
            {
                wprintf (L"\tNo registry objects found\n");
            }

            pEnum->Release();
        }
    }

For more information about MMC, see the Microsoft Management Console.