コード例 #1
0
        /// <summary>
        /// Try to find devices by Vendor Id and Product Id
        /// </summary>
        /// <param name="vid">Vendor Id. Set pid to 0 to find devices with any Product Id</param>
        /// <param name="pid">Product Id. Set vid and pid to 0 to find any devices</param>
        /// <returns></returns>
        public List <HidDevice> SearchDevices(int vid, int pid)
        {
            var devices = new List <HidDevice>();

            var devicesLinkedList = HidApi.hid_enumerate((ushort)vid, (ushort)pid);

            devicesEnumerations.Add(devicesLinkedList);

            if (devicesLinkedList == IntPtr.Zero)
            {
                return(devices);
            }

            hid_device_info deviceInfo =
#if NET40
                (hid_device_info)Marshal.PtrToStructure(devicesLinkedList, typeof(hid_device_info));
#endif
#if NETSTANDARD2_0
                Marshal.PtrToStructure <hid_device_info>(devicesLinkedList);
#endif

                devices.Add(new HidDevice(deviceInfo, devicesLinkedList));

            while (deviceInfo.next != IntPtr.Zero && devices.Count < MAX_DEVICE_COUNT)
            {
                var ptr = deviceInfo.next;

                deviceInfo =
#if NET40
                    (hid_device_info)Marshal.PtrToStructure(deviceInfo.next, typeof(hid_device_info));
#endif
#if NETSTANDARD2_0
                    Marshal.PtrToStructure <hid_device_info>(deviceInfo.next);
#endif

                    devices.Add(new HidDevice(deviceInfo, ptr));
            }

            if (devices.Count >= MAX_DEVICE_COUNT)
            {
                throw new Exception($"device max number was reached while enumerating devices - {MAX_DEVICE_COUNT}");
            }

            return(devices);
        }
コード例 #2
0
ファイル: HidDevice.cs プロジェクト: nvchernov/hidapiadapter
 internal HidDevice(hid_device_info deviceInfo, IntPtr devicePtr)
 {
     m_DeviceInfo = deviceInfo;
     m_DevicePtr  = devicePtr;
 }