Trying to use Windows Setupapi.h functions to obtain an error code shown in device properties

Viewed 83

I'm working with an HP Reverb G2 headset. If it's not plugged into a USB 3.0 port, it does not work. The device shows up in device manager, but right clicking it and selecting properties shows the following device error: Reverb G2 properties

In that picture, the "Device status" text box on the "General" tab of the device's properties reads:

This device is working properly.

Device Error : 0x80040203 : E_DEVICE_USB_SPEED_TOO_SLOW

I need to be able to detect this specific state in code (C++), but I haven't figured out a way yet. Using the functions in "Setupapi.h", I'm able to find the right device. I can then query for different info using SetupDiGetDevicePropertyW. But I haven't found anything that gives me the info I need.

I was hoping querying DEVPKEY_Device_ProblemCode would do what I needed, but it's not returning any problem. I suppose because the above quote does start with "This device is working properly."

Is there any way for me to obtain that device error code, or the entire contents of that text box?

Here's sample code querying for the Problem Code:

long GetReverbProblemCode()
{
    long returnValue = -1;

    HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES);
    if (hDevInfo != INVALID_HANDLE_VALUE)
    {
        SP_DEVINFO_DATA deviceInfoData;
        ZeroMemory(&deviceInfoData, sizeof(SP_DEVINFO_DATA));
        deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

        int devindex = 0;
        while (SetupDiEnumDeviceInfo(hDevInfo, devindex, &deviceInfoData))
        {
            devindex++;

            DWORD requiredSize = 0;
            DEVPROPTYPE ulPropertyType;
            ZeroMemory(&gVRNative_szBuffer[0], sizeof(gVRNative_szBuffer));
            if (SetupDiGetDevicePropertyW(hDevInfo, &deviceInfoData, &DEVPKEY_Device_FriendlyName, &ulPropertyType, (BYTE*)gVRNative_szBuffer, sizeof(gVRNative_szBuffer), &requiredSize, 0))
            {
                if (_wcsicmp(gVRNative_szBuffer, L"HP Reverb Virtual Reality Headset G2") == 0)
                {
                    ZeroMemory(&propertyBuffer[0], sizeof(propertyBuffer));
                    if (SetupDiGetDevicePropertyW(hDevInfo, &deviceInfoData, &DEVPKEY_Device_ProblemCode, &ulPropertyType, (BYTE*)propertyBuffer, sizeof(propertyBuffer), &requiredSize, 0))
                    {
                        unsigned long deviceProblemCode = *((unsigned long*)propertyBuffer);
                        returnValue = deviceProblemCode;
                    }
                    break;
                }
            }
        }
        SetupDiDestroyDeviceInfoList(hDevInfo);
    }

    return returnValue;
}
1 Answers

I should have kept looking a little longer. Just needed to query for DEVPKEY_Device_DriverProblemDesc. That gives me a string containing the error text from the text box in the device's properties window:

Device Error : 0x80040203 : E_DEVICE_USB_SPEED_TOO_SLOW

Adjusted function call:

SetupDiGetDevicePropertyW(hDevInfo, &deviceInfoData, &DEVPKEY_Device_DriverProblemDesc, &ulPropertyType, (BYTE*)propertyBuffer, sizeof(propertyBuffer), &requiredSize, 0);
Related