示例#1
0
 private void Initialise(string strPath)
 {
     this.m_hHandle = Win32Usb.CreateFile(strPath, 0xc0000000, 3, IntPtr.Zero, 3, 0x40000000, IntPtr.Zero);
     if (this.m_hHandle != Win32Usb.InvalidHandleValue)
     {
         IntPtr ptr;
         if (Win32Usb.HidD_GetPreparsedData(this.m_hHandle, out ptr))
         {
             try
             {
                 try
                 {
                     Win32Usb.HidCaps caps;
                     Win32Usb.HidP_GetCaps(ptr, out caps);
                     this.m_nInputReportLength  = caps.InputReportByteLength;
                     this.m_nOutputReportLength = caps.OutputReportByteLength;
                     this.m_oFile = new FileStream(new SafeFileHandle(this.m_hHandle, false), FileAccess.ReadWrite, this.m_nInputReportLength, true);
                     this.BeginAsyncRead();
                 }
                 catch (Exception)
                 {
                     throw HIDDeviceException.GenerateWithWinError("Failed to get the detailed data from the hid.");
                 }
                 return;
             }
             finally
             {
                 Win32Usb.HidD_FreePreparsedData(ref ptr);
             }
         }
         throw HIDDeviceException.GenerateWithWinError("GetPreparsedData failed");
     }
     this.m_hHandle = IntPtr.Zero;
     throw HIDDeviceException.GenerateWithWinError("Failed to create device file");
 }
示例#2
0
        // Token: 0x06000024 RID: 36 RVA: 0x00003CB0 File Offset: 0x00001EB0
        private void Initialise(string strPath)
        {
            this.m_hHandle = Win32Usb.CreateFile(strPath, 3221225472u, 0u, IntPtr.Zero, 3u, 1073741824u, IntPtr.Zero);
            if (!(this.m_hHandle != Win32Usb.InvalidHandleValue))
            {
                this.m_hHandle = IntPtr.Zero;
                throw HIDDeviceException.GenerateWithWinError("Failed to create device file");
            }
            IntPtr lpData;

            if (Win32Usb.HidD_GetPreparsedData(this.m_hHandle, out lpData))
            {
                try
                {
                    Win32Usb.HidCaps hidCaps;
                    Win32Usb.HidP_GetCaps(lpData, out hidCaps);
                    this.m_nInputReportLength  = (int)hidCaps.InputReportByteLength;
                    this.m_nOutputReportLength = (int)hidCaps.OutputReportByteLength;
                    HIDDevice.OutputLength     = this.m_nOutputReportLength;
                    HIDDevice.InputLength      = this.m_nInputReportLength;
                    this.m_oFile = new FileStream(new SafeFileHandle(this.m_hHandle, false), FileAccess.ReadWrite, this.m_nInputReportLength, true);
                    this.BeginAsyncRead();
                }
                catch (Exception ex)
                {
                    throw HIDDeviceException.GenerateWithWinError("Failed to get the detailed data from the hid." + ex.ToString());
                }
                finally
                {
                    Win32Usb.HidD_FreePreparsedData(ref lpData);
                }
                return;
            }
            throw HIDDeviceException.GenerateWithWinError("GetPreparsedData failed");
        }
示例#3
0
        /// <summary>
        /// Initialises the device
        /// </summary>
        /// <param name="strPath">Path to the device</param>
        private void Initialise(string strPath)
        {
            // Create the file from the device path
            m_hHandle = CreateFile(strPath,
                                   GENERIC_READ | GENERIC_WRITE,
                                   FILE_SHARE_WRITE | FILE_SHARE_READ,
                                   IntPtr.Zero,
                                   OPEN_EXISTING,
                                   FILE_FLAG_OVERLAPPED,
                                   IntPtr.Zero);

            if (m_hHandle != InvalidHandleValue || m_hHandle == null)                   // if the open worked...
            {
                IntPtr lpData;
                if (HidD_GetPreparsedData(m_hHandle, out lpData))                       // get windows to read the device data into an internal buffer
                {
                    try
                    {
                        HidCaps oCaps;
                        HidP_GetCaps(lpData, out oCaps);                                // extract the device capabilities from the internal buffer
                        m_nInputReportLength  = oCaps.InputReportByteLength;            // get the input...
                        m_nOutputReportLength = oCaps.OutputReportByteLength;           // ... and output report lengths

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

                        BeginAsyncRead();                               // kick off the first asynchronous read
                    }
                    catch (Exception ex)
                    {
                        throw HIDDeviceException.GenerateWithWinError("Failed to get the detailed data from the hid.");
                    }
                    finally
                    {
                        HidD_FreePreparsedData(ref lpData);                             // before we quit the funtion, we must free the internal buffer reserved in GetPreparsedData
                    }
                }
                else                    // GetPreparsedData failed? Chuck an exception
                {
                    throw HIDDeviceException.GenerateWithWinError("GetPreparsedData failed");
                }
            }
            else                // File open failed? Chuck an exception
            {
                m_hHandle = IntPtr.Zero;
                throw HIDDeviceException.GenerateWithWinError("Failed to create device file");
            }
        }
示例#4
0
        /// <summary>
        /// Connect to device
        /// </summary>
        public bool Connect(string devicePath, bool useAsyncRead)
        {
            if (m_isConnected)
            {
                return(false);
            }

            // Create the file from the device path
            m_hHandle = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, IntPtr.Zero);

            if (m_hHandle != InvalidHandleValue || m_hHandle == null)                   // if the open worked...
            {
                IntPtr lpData;
                if (HidD_GetPreparsedData(m_hHandle, out lpData))                       // get windows to read the device data into an internal buffer
                {
                    try
                    {
                        // Get HID device informations
                        m_devicePath         = devicePath;
                        m_manufacturerString = GetManufacturerString(m_hHandle);
                        m_productString      = GetProductString(m_hHandle);
                        m_serialNumberString = GetSerialNumber(m_hHandle);

                        HIDD_ATTRIBUTES attributes;
                        GetAttr(m_hHandle, out attributes);
                        m_version   = attributes.VersionNumber;
                        m_vendorId  = attributes.VendorID;
                        m_productId = attributes.ProductID;

                        HidP_Caps oCaps;
                        HidP_GetCaps(lpData, out oCaps);                                // extract the device capabilities from the internal buffer
                        m_inputReportLength  = oCaps.InputReportByteLength;             // get the input...
                        m_outputReportLength = oCaps.OutputReportByteLength;            // ... and output report lengths


                        //m_oFile = new FileStream(m_hHandle, FileAccess.Read | FileAccess.Write, true, m_nInputReportLength, true);
                        m_oFile = new FileStream(new SafeFileHandle(m_hHandle, false), FileAccess.Read | FileAccess.Write, m_inputReportLength, useAsyncRead);
                    }
                    catch (Exception ex)
                    {
                        throw HIDDeviceException.GenerateWithWinError("Failed to get the detailed data from the hid.");
                    }
                    finally
                    {
                        HidD_FreePreparsedData(ref lpData);                             // before we quit the funtion, we must free the internal buffer reserved in GetPreparsedData
                    }
                }
                else                    // GetPreparsedData failed? Chuck an exception
                {
                    throw HIDDeviceException.GenerateWithWinError("GetPreparsedData failed");
                }
            }
            else                // File open failed? Chuck an exception
            {
                m_hHandle = IntPtr.Zero;
                throw HIDDeviceException.GenerateWithWinError("Failed to create device file");
            }

            m_useAsyncReads = useAsyncRead;
            m_isConnected   = true;

            if (m_useAsyncReads)
            {
                BeginAsyncRead();                               // kick off the first asynchronous read
            }
            return(true);
        }