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>
        /// Initialises the device
        /// </summary>
        /// <param name="strPath">Path to the device</param>
        private void Initialise(string strPath)
        {
            m_hHandle      = CreateFile(strPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, IntPtr.Zero);
            m_hWriteHandle = CreateFile(strPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, IntPtr.Zero);

            if (m_hHandle != InvalidHandleValue || m_hHandle == null)              // m_hWriteHandle ������� �����������
            {
                IntPtr lpData;
                if (HidD_GetPreparsedData(m_hHandle, out lpData))
                {
                    try
                    {
                        HidCaps oCaps;
                        HidP_GetCaps(lpData, out oCaps);
                        InputReportLength   = oCaps.InputReportByteLength;
                        OutputReportLength  = oCaps.OutputReportByteLength;
                        FeatureReportLength = oCaps.FeatureReportByteLength;

                        if (OutputReportLength > 0)
                        {
                            m_oFile = new FileStream(new SafeFileHandle(m_hWriteHandle, false), FileAccess.Write, OutputReportLength, true);
                        }

                        if (InputReportLength > 0)
                        {
                            m_iFile = new FileStream(new SafeFileHandle(m_hHandle, false), FileAccess.Read, InputReportLength, true);
                            //HidD_FlushQueue(m_hHandle);
                            BeginAsyncRead();
                        }
                    }
                    catch (Exception ex)
                    {
                        throw HIDDeviceException.GenerateWithWinError("Failed to create FileStream to device.");
                    }
                    finally
                    {
                        HidD_FreePreparsedData(ref lpData);
                    }
                }
                else
                {
                    throw HIDDeviceException.GenerateWithWinError("GetPreparsedData failed");
                }
            }
            else
            {
                m_hHandle      = IntPtr.Zero;
                m_hWriteHandle = IntPtr.Zero;
                throw HIDDeviceException.GenerateWithWinError("Failed to create device file");
            }
        }
        /// <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);
        }