如何枚举所有HID设备? C#

我需要枚举连接到我的PC的所有HID设备。 我尝试使用这个答案 ,但它枚举USBHub设备,我找不到我的HID设备。

编辑 :我很高兴知道如果有任何WIN32 API方法,使用PID和VID获取USB设备状态(在线/离线)?

我找到了答案。 此链接说明了如何使用ManagementObjectSearcher执行此操作。

感谢所有回复的人!

Microsoft的WDK包含HIDfunction的文档以及如何使用它们的概述。 WDK还包括用于访问HID类设备的Visual C ++程序的头文件(hidsdi.h,hidusage.h,hidpi.h)。

查看此链接Jan Axelson的Lakeview Research – HID Windows编程。

以下是您在问题中指定的有关HID设备的问题: 使用C#扫描人机接口设备(HID)

您可以使用以下Windows API枚举Hid设备:

  public static Collection GetConnectedDeviceInformations() { var deviceInformations = new Collection(); var spDeviceInterfaceData = new SpDeviceInterfaceData(); var spDeviceInfoData = new SpDeviceInfoData(); var spDeviceInterfaceDetailData = new SpDeviceInterfaceDetailData(); spDeviceInterfaceData.CbSize = (uint)Marshal.SizeOf(spDeviceInterfaceData); spDeviceInfoData.CbSize = (uint)Marshal.SizeOf(spDeviceInfoData); var hidGuid = new Guid(); APICalls.HidD_GetHidGuid(ref hidGuid); var i = APICalls.SetupDiGetClassDevs(ref hidGuid, IntPtr.Zero, IntPtr.Zero, APICalls.DigcfDeviceinterface | APICalls.DigcfPresent); if (IntPtr.Size == 8) { spDeviceInterfaceDetailData.CbSize = 8; } else { spDeviceInterfaceDetailData.CbSize = 4 + Marshal.SystemDefaultCharSize; } var x = -1; while (true) { x++; var setupDiEnumDeviceInterfacesResult = APICalls.SetupDiEnumDeviceInterfaces(i, IntPtr.Zero, ref hidGuid, (uint)x, ref spDeviceInterfaceData); var errorNumber = Marshal.GetLastWin32Error(); //TODO: deal with error numbers. Give a meaningful error message if (setupDiEnumDeviceInterfacesResult == false) { break; } APICalls.SetupDiGetDeviceInterfaceDetail(i, ref spDeviceInterfaceData, ref spDeviceInterfaceDetailData, 256, out _, ref spDeviceInfoData); var deviceInformation = GetDeviceInformation(spDeviceInterfaceDetailData.DevicePath); if (deviceInformation == null) { continue; } deviceInformations.Add(deviceInformation); } APICalls.SetupDiDestroyDeviceInfoList(i); return deviceInformations; } 

全class: https : //github.com/MelbourneDeveloper/Hid.Net/blob/master/Hid.Net/Windows/WindowsHIDDevice.cs

APIS: https : //github.com/MelbourneDeveloper/Hid.Net/blob/master/Hid.Net/Windows/APICalls.cs