【发布时间】:2011-12-02 10:09:41
【问题描述】:
我需要枚举连接到我的 PC 的所有 HID 设备。我尝试使用this answer,但它枚举了 USBHub 设备,我无法找到我的 HID 设备。
编辑: 我很高兴知道是否有任何 WIN32 API 方法,使用 PID 和 VID 获取 USB 设备状态(在线/离线)?
【问题讨论】:
我需要枚举连接到我的 PC 的所有 HID 设备。我尝试使用this answer,但它枚举了 USBHub 设备,我无法找到我的 HID 设备。
编辑: 我很高兴知道是否有任何 WIN32 API 方法,使用 PID 和 VID 获取 USB 设备状态(在线/离线)?
【问题讨论】:
我找到了答案。 This link 解释了如何使用 ManagementObjectSearcher 执行此操作。
感谢所有回复的人!
【讨论】:
Microsoft's WDK 有 HID 功能的文档以及如何使用它们的概述。 WDK 还包括用于访问 HID 类设备(hidsdi.h、hidusage.h、hidpi.h)的 Visual C++ 程序的头文件。
查看此链接Jan Axelson's Lakeview Research - HID Windows Programming.
这里还有一个关于您在问题中指定的 HID 设备的问题: Scanning for a Human Interface Device (HID) using C#
【讨论】:
您可以像这样使用 Windows API 枚举隐藏设备:
public static Collection<DeviceInformation> GetConnectedDeviceInformations()
{
var deviceInformations = new Collection<DeviceInformation>();
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;
}
全班:https://github.com/MelbourneDeveloper/Hid.Net/blob/master/Hid.Net/Windows/WindowsHIDDevice.cs
API:https://github.com/MelbourneDeveloper/Hid.Net/blob/master/Hid.Net/Windows/APICalls.cs
【讨论】: