How to get human readable name for RawInput HID de

2020-06-12 06:13发布

问题:

I'm switching an application from DirectInput to RawInput for gamepad handling, and I'd like to present a human readable description for each gamepad. The ideal would be the device text that appears in device manager, but the USB product description would also do. Any method should work without administrator permission.

So far I've found one set of clues: there seems to be a text field in the registry under HKLM\SYSTEM\CurrentControlSet\Control\MediaProperties\PrivateProperties\Joystick\OEM which is the same as the name provided by DirectInput. This isn't perfect - I have a gamepad that appears in English in device manager, but lists as 氀 in the registry.

Is there a way to get from the HANDLE provided by WM_INPUT to the device manager description? HidD_GetProductString looks promising, but I don't know how to get the Hid top level collection handle from the RawInput handle.

Edit: I have got a device instance path (eg. \\?\HID#VID_1267&PID_A001#8&1d630df6&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}, which may be enough to extract the information I need from SetupAPI. Does anybody know how to reach this given an instance path?

回答1:

I've been having a similar problem and believe I found a potential solution.

It seems you must call CreateFile with the name that RawInput provides from calling GetRawInputDeviceInfo with RIDI_DEVICENAME as the uiCommand parameter's argument. This will give you a handle to the device with which you may call HidD_GetProductString.

wchar_t DeviceName[126];
HANDLE HIDHandle = CreateFile(RawInputDeviceName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
if(HIDHandle)
{
    BOOLEAN Result = HidD_GetProductString(HIDHandle, DeviceName, sizeof(wchar_t) * 126);
    CloseHandle(HIDHandle);
}

However, it seems that HidD_GetProductString fails on a majority of the devices attached on my system and only seems to succeed for my USB keyboard and a web camera. It does not succeed for my USB mouse. I have not yet discovered why this is, but perhaps my progress will aid you.



回答2:

You get a handle to the top level collection as explained here: https://msdn.microsoft.com/en-us/library/windows/hardware/ff538731%28v=vs.85%29.aspx