예제 #1
0
        public static List <HidInfo> Browse()
        {
            Guid           gHid;
            List <HidInfo> info = new List <HidInfo>();

            // Obtain hid guid
            Native.HidD_GetHidGuid(out gHid);
            // Get list of present hid devices
            var hInfoSet = Native.SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, Native.DIGCF_DEVICEINTERFACE | Native.DIGCF_PRESENT);

            // Allocate mem for interface descriptor
            var iface = new Native.DeviceInterfaceData();

            // Set size field
            iface.Size = Marshal.SizeOf(iface);
            // Interface index
            uint index = 0;

            // iterate through all interfaces
            while (Native.SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, index, ref iface))
            {
                short vid, pid;

                var path = GetPath(hInfoSet, ref iface);

                var handle = Open(path);
                if (handle != Native.INVALID_HANDLE_VALUE)
                {
                    var man    = GetManufacturer(handle);
                    var prod   = GetProduct(handle);
                    var serial = GetSerialNumber(handle);
                    GetVidPid(handle, out vid, out pid);
                    var cap = GetDeviceCapabilities(handle);

                    HidInfo i = new HidInfo(prod, serial, man, path, vid, pid, cap);
                    info.Add(i);

                    Close(handle);
                }

                index++;
            }

            // Clean up
            if (Native.SetupDiDestroyDeviceInfoList(hInfoSet) == false)
            {
                throw new Win32Exception();
            }

            return(info);
        }
예제 #2
0
        private static string GetPath(IntPtr hInfoSet, ref Native.DeviceInterfaceData iface)
        {
            var  detIface = new Native.DeviceInterfaceDetailData();
            uint reqSize  = (uint)Marshal.SizeOf(detIface);

            /* Set size. The cbSize member always contains the size of the
             * fixed part of the data structure, not a size reflecting the
             * variable-length string at the end. */
            detIface.Size = Marshal.SizeOf(typeof(IntPtr)) == 8 ? 8 : 5;

            bool status = Native.SetupDiGetDeviceInterfaceDetail(hInfoSet, ref iface, ref detIface, reqSize, ref reqSize, IntPtr.Zero);

            if (!status)
            {
                throw new Win32Exception();
            }

            return(detIface.DevicePath);
        }