Problem when getting list of devices using WMI in C++

I'm trying to get a list of device name from the WMI. I'm using this example. The example itself works fine, but when I modify it, it starts doing things. This is the segment of the code that I've changed:

    // Step 6: --------------------------------------------------
    // Use the IWbemServices pointer to make requests of WMI ----

    // Selecting all device info from Win32_PnPEntity
    IEnumWbemClassObject* pEnumerator = NULL;
    hres = pSvc->ExecQuery(
        bstr_t("WQL"),
        bstr_t("SELECT * FROM Win32_PnPSignedDriver"),
        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
        NULL,
        &pEnumerator);

    if (FAILED(hres))
    {
        cout << "Query for device info list failed."
            << " Error code = 0x"
            << hex << hres << endl;
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        return 1;               // Program has failed.
    }

    // Step 7: -------------------------------------------------
    // Get the data from the query in step 6 -------------------

    IWbemClassObject *pclsObj = NULL;
    ULONG uReturn = 0;

    while (pEnumerator)
    {
        HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
            &pclsObj, &uReturn);

        if (0 == uReturn)
        {
            break;
        }

        VARIANT vtProp;

        // Get the value of the Name property
        hr = pclsObj->Get(L"DeviceName", 0, &vtProp, 0, 0);
        wcout << "Device Name : " << vtProp.bstrVal << endl;
        VariantClear(&vtProp);

        pclsObj->Release();
    }

    // Cleanup
    // ========

    pSvc->Release();
    pLoc->Release();
    pEnumerator->Release();
    CoUninitialize();

    cout << "FINNISHED" << endl; //debug

    return 0;   // Program successfully completed.

}

When I run it, I get this:

Connected to ROOT\CIMV2 WMI namespace
Device Name : FINISHED

C:\Users\sash\source\repos\ConsoleApplication6\Debug\ConsoleApplication6.exe (процесс 9200) завершает работу с кодом 0.

When I ask for DeviceID, it shows like twenty devices and stops in a same manner(this is just the end of that output):

//A lot of IDs
...
DeviceID : ROOT\*ISATAP\0002
DeviceID : ROOT\*ISATAP\0000
DeviceID : HTREE\ROOT\0
DeviceID : FINISHED

C:\Users\sash\source\repos\ConsoleApplication6\Debug\ConsoleApplication6.exe (процесс 7148) завершает работу с кодом 0.

I have tried using Win32_PnPEntity, and gets me a little bit more:

Connected to ROOT\CIMV2 WMI namespace
Name : Atheros Bluetooth Bus
Name : FINISHED

C:\Users\sash\source\repos\ConsoleApplication6\Debug\ConsoleApplication6.exe (процесс 8292) завершает работу с кодом 0.

The russian text in both of these says it returned 0. I've found a VB script, that does the same thing, and it gets the info just fine, so I really don't understand what might be the problem.

Here's that VB script just in case:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * from Win32_PnPSignedDriver")

For Each objItem in colItems
    Wscript.Echo "Device ID: " & objItem.DeviceID & " Device Name: " & objItem.DeviceName & " Description: " & objItem.Description & " ENDDEVICE"
   Wscript.Echo
Next

It also seems to be a little bit selective with this, when I make query for ClassGuid it seems to output it for all devices.

1 answer

  • answered 2018-11-08 11:28 Sansanichick

    I'm an idiot, the code works completely fine, just some of the devices have russian words in them, so I had to set locale to russian, and now it works. I feel very stupid.