public static List <HIDDeviceAvalible> GetDevicesListByVID(int nVid)
        {
            List <HIDDeviceAvalible> devices = new List <HIDDeviceAvalible>();
            string strSearch = string.Format("vid_{0:x4}&pid_", nVid);
            Guid   gHid;

            HidD_GetHidGuid(out gHid);

            IntPtr hInfoSet = SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);

            try
            {
                uint nIndex = 0;
                DeviceInterfaceData oInterface = new DeviceInterfaceData();
                oInterface.Size = Marshal.SizeOf(oInterface);

                while (SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, nIndex, ref oInterface))
                {
                    string strDevicePath = GetDevicePath(hInfoSet, ref oInterface);
                    if (strDevicePath.IndexOf(strSearch) >= 0)
                    {
                        HIDDeviceAvalible device = new HIDDeviceAvalible();
                        device.Path        = strDevicePath;
                        device.Description = HIDDevice.GetProductString(device.Path);

                        int    cut       = device.Path.IndexOf("vid_");
                        string xid_start = device.Path.Substring(cut + 4);
                        device.VendorId = Convert.ToUInt16(xid_start.Substring(0, 4), 16);

                        cut              = device.Path.IndexOf("pid_");
                        xid_start        = device.Path.Substring(cut + 4);
                        device.ProductId = Convert.ToUInt16(xid_start.Substring(0, 4), 16);

                        if (!String.IsNullOrEmpty(device.Description))
                        {
                            devices.Add(device);
                        }
                    }
                    nIndex++;
                }
            }
            catch (Exception ex)
            {
                throw HIDDeviceException.GenerateError(ex.ToString());
            }
            finally
            {
                SetupDiDestroyDeviceInfoList(hInfoSet);
            }

            return(devices);
        }
 public static HIDDevice OpenDevice(string device_path, Type oType)
 {
     try
     {
         HIDDevice oNewDevice = (HIDDevice)Activator.CreateInstance(oType);
         oNewDevice.Initialise(device_path);
         return(oNewDevice);
     }
     catch (Exception ex)
     {
         throw HIDDeviceException.GenerateError(ex.ToString());
     }
 }
        /// <summary>
        /// Finds a device given its PID and VID
        /// </summary>
        /// <param name="nVid">Vendor id for device (VID)</param>
        /// <param name="nPid">Product id for device (PID)</param>
        /// <param name="oType">Type of device class to create</param>
        /// <returns>A new device class of the given type or null</returns>
        public static HIDDevice FindDevice(int nVid, int nPid, Type oType)
        {
            string strPath   = string.Empty;
            string strSearch = string.Format("vid_{0:x4}&pid_{1:x4}", nVid, nPid);
            Guid   gHid      = HIDGuid;

            HidD_GetHidGuid(out gHid);

            IntPtr hInfoSet = SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);

            try
            {
                int nIndex = 0;
                DeviceInterfaceData oInterface = new DeviceInterfaceData();
                oInterface.Size = Marshal.SizeOf(oInterface);

                while (SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, (uint)nIndex, ref oInterface))
                {
                    string strDevicePath = GetDevicePath(hInfoSet, ref oInterface);
                    if (strDevicePath.IndexOf(strSearch) >= 0)
                    {
                        HIDDevice oNewDevice = (HIDDevice)Activator.CreateInstance(oType);
                        oNewDevice.Initialise(strDevicePath);
                        return(oNewDevice);
                    }
                    nIndex++;
                }
            }
            catch (Exception ex)
            {
                throw HIDDeviceException.GenerateError(ex.ToString());
            }
            finally
            {
                SetupDiDestroyDeviceInfoList(hInfoSet);
            }
            return(null);
        }