对于 HID 设备,您可以使用HidD_GetProductString
,但键盘和鼠标不能通过 Windows 上的 HID API 使用,并作为单独的设备类型公开:GUID_DEVINTERFACE_KEYBOARD / GUID_DEVINTERFACE_MOUSE(出于安全原因,用户模式应用程序无法直接读取它们)。
您可以从设备接口符号链接获取他们的设备信息,如下所示:
使用CM_Get_Device_Interface_Property
或SetupDiGetDeviceInterfaceProperty
withDEVPKEY_Device_InstanceId
获取设备实例 ID(一个设备可以有多个接口)。
获得设备实例 ID 后,您可以使用CM_Get_DevNode_Property
或SetupDiGetDeviceProperty
withDEVPKEY_NAME
来获取设备的本地化友好名称(显示在设备管理器中)。
这是我的测试仓库CM_*
中通过API的示例代码:
bool FillDeviceInfo(const std::wstring& deviceInterfaceName)
{
// you need to provide deviceInterfaceName
// example from my system: `\\?\HID#VID_203A&PID_FFFC&MI_01#7&2de99099&0&0000#{378de44c-56ef-11d1-bc8c-00a0c91405dd}`
DEVPROPTYPE propertyType;
ULONG propertySize = 0;
CONFIGRET cr = ::CM_Get_Device_Interface_PropertyW(deviceInterfaceName.c_str(), &DEVPKEY_Device_InstanceId, &propertyType, nullptr, &propertySize, 0);
if (cr != CR_BUFFER_SMALL)
return false;
std::wstring deviceId;
deviceId.resize(propertySize);
cr = ::CM_Get_Device_Interface_PropertyW(deviceInterfaceName.c_str(), &DEVPKEY_Device_InstanceId, &propertyType, (PBYTE)deviceId.data(), &propertySize, 0);
if (cr != CR_SUCCESS)
return false;
// here is deviceId will contain device instance id
// example from my system: `HID\VID_203A&PID_FFFC&MI_01\7&2de99099&0&0000`
DEVINST devInst;
cr = ::CM_Locate_DevNodeW(&devInst, (DEVINSTID_W)deviceId.c_str(), CM_LOCATE_DEVNODE_NORMAL);
if (cr != CR_SUCCESS)
return false;
propertySize = 0;
cr = ::CM_Get_DevNode_PropertyW(devInst, &DEVPKEY_NAME, &propertyType, nullptr, &propertySize, 0);
if (cr != CR_BUFFER_SMALL)
return false;
std::wstring friendlyString;
friendlyString.resize(propertySize);
cr = ::CM_Get_DevNode_PropertyW(devInst, &DEVPKEY_NAME, &propertyType, (PBYTE)friendlyString.data(), &propertySize, 0);
// here is friendlyString will contain localized device friendly name
propertySize = 0;
cr = ::CM_Get_DevNode_PropertyW(devInst, &DEVPKEY_Device_Manufacturer, &propertyType, nullptr, &propertySize, 0);
if (cr != CR_BUFFER_SMALL)
return false;
std::wstring manufacturer;
manufacturer.resize(propertySize);
cr = ::CM_Get_DevNode_PropertyW(devInst, &DEVPKEY_Device_Manufacturer, &propertyType, (PBYTE)manufacturer.data(), &propertySize, 0);
// here is manufacturer will contain localized device "manufacturer-identifier"
return true;
}
更新:原来HidD_GetProductString
和家人正在为 HID 键盘和鼠标工作。您只需以只读方式打开其设备接口(路径)。例如,请参阅https://github.com/DJm00n/RawInputDemo/blob/master/RawInputLib/RawInputDevice.cpp#L77-L86。