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

发布于 2024-12-19 07:38:08 字数 220 浏览 2 评论 0原文

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

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

I need to enumerate all HID devices connected to my PC. I tried to use this answer, but it enumerates USBHub devices and I can't find there my HID device.

EDIT:
I will be glad to know If there any WIN32 API method, to get USB device status (online/offline) using PID and VID?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对你的占有欲 2024-12-26 07:38:08

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

感谢所有回复的人!

I found the answer. This link explains how to do this with ManagementObjectSearcher.

Thanks for all who replied!

夜声 2024-12-26 07:38:08

Microsoft 的 WDK 提供了 HID 功能的文档和概述如何使用它们。 WDK 还包括与访问 HID 类设备的 Visual C++ 程序一起使用的头文件(hidsdi.h、hidusage.h、hidpi.h)。

检查此链接 Jan Axelson 的 Lakeview 研究 - HID Windows 编程。

这里还有一个关于 HID 设备的问题:您在问题中指定:
使用 C# 扫描人机接口设备 (HID)< /a>

Microsoft's WDK has documentation for the HID functions and an overview of how to use them. The WDK also includes the header files to use with Visual C++ programs that access HID-class devices (hidsdi.h, hidusage.h, hidpi.h).

Check this link Jan Axelson's Lakeview Research - HID Windows Programming.

Here is an question also available regarding HID devices as you specified in your question:
Scanning for a Human Interface Device (HID) using C#

北笙凉宸 2024-12-26 07:38:08

您可以使用 Windows API 枚举 Hid 设备,如下所示:

        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

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

You can enumerate Hid devices with Windows APIs like this:

        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;
}

Full 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文