コード例 #1
0
        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;
            IntPtr hInfoSet  = SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);

            try
            {
                DeviceInterfaceData oInterface = new DeviceInterfaceData();
                oInterface.Size = Marshal.SizeOf(oInterface);
                int nIndex = 0;
                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);
            }
            // No device found.
            return(null);
        }
コード例 #2
0
        private void Initialise(string strPath)
        {
            m_hHandle = CreateFile(strPath, GENERIC_READ | GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, IntPtr.Zero);

            if (m_hHandle != InvalidHandleValue || m_hHandle == null)
            {
                IntPtr lpData;
                if (HidD_GetPreparsedData(m_hHandle, out lpData))
                {
                    try
                    {
                        HidCaps oCaps;
                        HidP_GetCaps(lpData, out oCaps);
                        m_nInputReportLength  = oCaps.InputReportByteLength;
                        m_nOutputReportLength = oCaps.OutputReportByteLength;

                        m_oFile = new FileStream(new SafeFileHandle(m_hHandle, false), FileAccess.Read | FileAccess.Write, m_nInputReportLength, true);

                        BeginAsyncRead();
                    }
                    catch (Exception)
                    {
                        throw HidDeviceException.GenerateWithWinError("Failed to get the detailed data from the hid.");
                    }
                    finally
                    {
                        HidD_FreePreparsedData(ref lpData);
                    }
                }
                else
                {
                    throw HidDeviceException.GenerateWithWinError("GetPreparsedData failed");
                }
            }
            else
            {
                m_hHandle = IntPtr.Zero;
                throw HidDeviceException.GenerateWithWinError("Failed to create device file");
            }
        }