How to enumerate all HID devices? C#

北慕城南 提交于 2019-12-07 05:28:20

问题


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?


回答1:


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

Thanks for all who replied!




回答2:


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#




回答3:


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



来源:https://stackoverflow.com/questions/8354700/how-to-enumerate-all-hid-devices-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!