ReadFile() private méthode

private ReadFile ( IntPtr hFile, [ lpBuffer, uint nNumberOfBytesToRead, uint &lpNumberOfBytesRead, IntPtr lpOverlapped ) : bool
hFile System.IntPtr
lpBuffer [
nNumberOfBytesToRead uint
lpNumberOfBytesRead uint
lpOverlapped System.IntPtr
Résultat bool
Exemple #1
0
 public ReadStatus ReadFile(byte[] inputBuffer)
 {
     if (safeReadHandle == null)
     {
         safeReadHandle = OpenHandle(_devicePath, true);
     }
     try
     {
         uint bytesRead;
         lock (this)
         {
             idleTicks = 0;
         }
         if (NativeMethods.ReadFile(safeReadHandle.DangerousGetHandle(), inputBuffer, (uint)inputBuffer.Length, out bytesRead, IntPtr.Zero))
         {
             return(ReadStatus.Success);
         }
         else
         {
             return(ReadStatus.NoDataRead);
         }
     }
     catch (Exception)
     {
         return(ReadStatus.ReadError);
     }
 }
Exemple #2
0
        public bool ReadFile(byte[] inputBuffer, bool isExclusive, out int error)
        {
            if ((safeReadHandle == null) || (safeReadHandle.IsInvalid))
            {
                if (safeReadHandle != null)
                {
                    safeReadHandle.Dispose();
                }

                safeReadHandle = OpenHandle(_devicePath, isExclusive);
            }

            try
            {
                uint bytesRead;
                lock (this)
                {
                    idleTicks = 0;
                }

                var readResult = NativeMethods.ReadFile(safeReadHandle.DangerousGetHandle(), inputBuffer, (uint)inputBuffer.Length, out bytesRead, IntPtr.Zero);

                if (readResult)
                {
                    error = 0;
                    return(true);
                }
                else
                {
                    error = Marshal.GetLastWin32Error();
                    return(false);
                }
            }
            catch (Exception)
            {
                error = 0;
                return(false);
            }
        }
Exemple #3
0
        protected HidDeviceData ReadData(int timeout)
        {
            var buffer = new byte[] { };
            var status = HidDeviceData.ReadStatus.NoDataRead;

            if (_deviceCapabilities.InputReportByteLength > 0)
            {
                uint bytesRead = 0;

                buffer = CreateInputBuffer();

                if (_deviceReadMode == DeviceMode.Overlapped)
                {
                    var security       = new NativeMethods.SECURITY_ATTRIBUTES();
                    var overlapped     = new NativeMethods.NativeOverlapped();
                    var overlapTimeout = timeout <= 0 ? NativeMethods.WAIT_INFINITE : timeout;

                    security.lpSecurityDescriptor = IntPtr.Zero;
                    security.bInheritHandle       = true;
                    security.nLength = Marshal.SizeOf(security);

                    overlapped.OffsetLow   = 0;
                    overlapped.OffsetHigh  = 0;
                    overlapped.EventHandle = NativeMethods.CreateEvent(ref security, Convert.ToInt32(false), Convert.ToInt32(true), string.Empty);

                    try
                    {
                        NativeMethods.ReadFile(Handle, buffer, (uint)buffer.Length, out bytesRead, ref overlapped);

                        var result = NativeMethods.WaitForSingleObject(overlapped.EventHandle, overlapTimeout);

                        switch (result)
                        {
                        case NativeMethods.WAIT_OBJECT_0: status = HidDeviceData.ReadStatus.Success; break;

                        case NativeMethods.WAIT_TIMEOUT:
                            status = HidDeviceData.ReadStatus.WaitTimedOut;
                            buffer = new byte[] { };
                            break;

                        case NativeMethods.WAIT_FAILED:
                            status = HidDeviceData.ReadStatus.WaitFail;
                            buffer = new byte[] { };
                            break;

                        default:
                            status = HidDeviceData.ReadStatus.NoDataRead;
                            buffer = new byte[] { };
                            break;
                        }
                    }
                    catch { status = HidDeviceData.ReadStatus.ReadError; }
                    finally { CloseDeviceIO(overlapped.EventHandle); }
                }
                else
                {
                    try
                    {
                        var overlapped = new NativeMethods.NativeOverlapped();

                        NativeMethods.ReadFile(Handle, buffer, (uint)buffer.Length, out bytesRead, ref overlapped);
                        status = HidDeviceData.ReadStatus.Success;
                    }
                    catch { status = HidDeviceData.ReadStatus.ReadError; }
                }
            }
            return(new HidDeviceData(buffer, status));
        }
Exemple #4
0
        protected HidDeviceData ReadData(int timeout)
        {
            byte[] array = new byte[0];
            HidDeviceData.ReadStatus status = HidDeviceData.ReadStatus.NoDataRead;
            if (_deviceCapabilities.InputReportByteLength > 0)
            {
                uint lpNumberOfBytesRead = 0u;
                array = CreateInputBuffer();
                if (_deviceReadMode != DeviceMode.Overlapped)
                {
                    try
                    {
                        NativeOverlapped lpOverlapped = default(NativeOverlapped);
                        NativeMethods.ReadFile(ReadHandle, array, (uint)array.Length, out lpNumberOfBytesRead, ref lpOverlapped);
                        status = HidDeviceData.ReadStatus.Success;
                    }
                    catch
                    {
                        status = HidDeviceData.ReadStatus.ReadError;
                    }
                }
                else
                {
                    NativeMethods.SECURITY_ATTRIBUTES securityAttributes = default(NativeMethods.SECURITY_ATTRIBUTES);
                    NativeOverlapped lpOverlapped2 = default(NativeOverlapped);
                    int dwMilliseconds             = (timeout <= 0) ? 65535 : timeout;
                    securityAttributes.lpSecurityDescriptor = IntPtr.Zero;
                    securityAttributes.bInheritHandle       = true;
                    securityAttributes.nLength = Marshal.SizeOf((object)securityAttributes);
                    lpOverlapped2.OffsetLow    = 0;
                    lpOverlapped2.OffsetHigh   = 0;
                    lpOverlapped2.EventHandle  = NativeMethods.CreateEvent(ref securityAttributes, Convert.ToInt32(false), Convert.ToInt32(true), string.Empty);
                    try
                    {
                        NativeMethods.ReadFile(ReadHandle, array, (uint)array.Length, out lpNumberOfBytesRead, ref lpOverlapped2);
                        switch (NativeMethods.WaitForSingleObject(lpOverlapped2.EventHandle, dwMilliseconds))
                        {
                        case 0u:
                            status = HidDeviceData.ReadStatus.Success;
                            break;

                        case 258u:
                            status = HidDeviceData.ReadStatus.WaitTimedOut;
                            array  = new byte[0];
                            break;

                        case uint.MaxValue:
                            status = HidDeviceData.ReadStatus.WaitFail;
                            array  = new byte[0];
                            break;

                        default:
                            status = HidDeviceData.ReadStatus.NoDataRead;
                            array  = new byte[0];
                            break;
                        }
                    }
                    catch
                    {
                        status = HidDeviceData.ReadStatus.ReadError;
                    }
                    finally
                    {
                        CloseDeviceIO(lpOverlapped2.EventHandle);
                    }
                }
            }
            return(new HidDeviceData(array, status));
        }
Exemple #5
0
        protected HidDeviceData ReadData(int timeout)
        {
            var buffer = new byte[] { };
            var status = HidDeviceData.ReadStatus.NoDataRead;

            if (_deviceCapabilities.InputReportByteLength > 0)
            {
                uint bytesRead = 0;

                buffer = CreateInputBuffer();

                if (_deviceReadMode == DeviceMode.Overlapped)
                {
                    var security       = new NativeMethods.SecurityAttributes();
                    var overlapped     = new NativeOverlapped();
                    var overlapTimeout = timeout <= 0 ? NativeMethods.WaitInfinite : timeout;

                    security.lpSecurityDescriptor = IntPtr.Zero;
                    security.bInheritHandle       = true;
                    security.nLength = Marshal.SizeOf(security);

                    overlapped.OffsetLow   = 0;
                    overlapped.OffsetHigh  = 0;
                    overlapped.EventHandle = NativeMethods.CreateEvent(ref security, Convert.ToInt32(true), Convert.ToInt32(true), string.Empty);

                    try
                    {
                        var resultReadFile = NativeMethods.ReadFile(Handle, buffer, (uint)buffer.Length, out bytesRead, ref overlapped);

                        if (!resultReadFile && Marshal.GetLastWin32Error() == NativeMethods.ErrorIOPending)
                        {
                            var resultWaitForSingleObject = NativeMethods.WaitForSingleObject(overlapped.EventHandle, overlapTimeout);

                            switch (resultWaitForSingleObject)
                            {
                            case NativeMethods.WaitObject0:
                            {
                                var resultGetOverlappedResult = NativeMethods.GetOverlappedResult(Handle, ref overlapped, out bytesRead, true);
                                NativeMethods.ResetEvent(overlapped.EventHandle);
                                status = HidDeviceData.ReadStatus.Success;
                                break;
                            }

                            case NativeMethods.WaitTimeout:
                                status = HidDeviceData.ReadStatus.WaitTimedOut;
                                buffer = new byte[] {};
                                break;

                            case NativeMethods.WaitFailed:
                                status = HidDeviceData.ReadStatus.WaitFail;
                                buffer = new byte[] {};
                                break;

                            default:
                                status = HidDeviceData.ReadStatus.NoDataRead;
                                buffer = new byte[] {};
                                break;
                            }
                        }
                        else
                        {
                            NativeMethods.ResetEvent(overlapped.EventHandle);
                        }
                    }
                    catch { status = HidDeviceData.ReadStatus.ReadError; }
                    finally { CloseDeviceIO(overlapped.EventHandle); }

                    /* ORIG!!
                     * overlapped.EventHandle = NativeMethods.CreateEvent(ref security, Convert.ToInt32(false), Convert.ToInt32(true), string.Empty);
                     * try
                     * {
                     *  NativeMethods.ReadFile(Handle, buffer, (uint)buffer.Length, out bytesRead, ref overlapped);
                     *
                     *  var result = NativeMethods.WaitForSingleObject(overlapped.EventHandle, overlapTimeout);
                     *
                     *  switch (result)
                     *  {
                     *      case NativeMethods.WAIT_OBJECT_0:
                     *      {
                     *          status = HidDeviceData.ReadStatus.Success;
                     *          break;
                     *      }
                     *      case NativeMethods.WAIT_TIMEOUT:
                     *          status = HidDeviceData.ReadStatus.WaitTimedOut;
                     *          buffer = new byte[] { };
                     *          break;
                     *      case NativeMethods.WAIT_FAILED:
                     *          status = HidDeviceData.ReadStatus.WaitFail;
                     *          buffer = new byte[] { };
                     *          break;
                     *      default:
                     *          status = HidDeviceData.ReadStatus.NoDataRead;
                     *          buffer = new byte[] { };
                     *          break;
                     *  }
                     * }
                     * catch { status = HidDeviceData.ReadStatus.ReadError; }
                     * finally { CloseDeviceIO(overlapped.EventHandle); }*/
                }
                else
                {
                    try
                    {
                        var overlapped = new NativeOverlapped();

                        NativeMethods.ReadFile(Handle, buffer, (uint)buffer.Length, out bytesRead, ref overlapped);
                        status = HidDeviceData.ReadStatus.Success;
                    }
                    catch { status = HidDeviceData.ReadStatus.ReadError; }
                }
            }
            return(new HidDeviceData(buffer, status));
        }
Exemple #6
0
        protected HidDeviceData ReadData(int timeout)
        {
            var buffer = new byte[] { };
            var status = HidDeviceData.ReadStatus.NoDataRead;

            if (_deviceCapabilities.InputReportByteLength > 0)
            {
                uint bytesRead = 0;

                //buffer = CreateInputBuffer();

                if (_deviceReadMode == DeviceMode.Overlapped)
                {
                    //IntPtr unManagedBuffer = IntPtr.Zero;
                    //IntPtr unManagedOverlapped = IntPtr.Zero;
                    var unManagedBytesRead = IntPtr.Zero;
                    var security           = new NativeMethods.SECURITY_ATTRIBUTES();
                    var overlapped         = new NativeOverlapped();
                    var overlapTimeout     = timeout <= 0 ? NativeMethods.WAIT_INFINITE : timeout;

                    security.lpSecurityDescriptor = IntPtr.Zero;
                    security.bInheritHandle       = true;
                    security.nLength = Marshal.SizeOf(security);

                    overlapped.OffsetLow   = 0;
                    overlapped.OffsetHigh  = 0;
                    overlapped.EventHandle = NativeMethods.CreateEvent(ref security, Convert.ToInt32(true),
                                                                       Convert.ToInt32(true), string.Empty);

                    try
                    {
                        var resultReadFile = NativeMethods.ReadFile(Handle, buffer, (uint)buffer.Length, out bytesRead, ref overlapped);

                        /*unManagedBuffer = Marshal.AllocHGlobal(buffer.Length);
                         * unManagedOverlapped = Marshal.AllocHGlobal(Marshal.SizeOf(overlapped));
                         * Marshal.StructureToPtr(overlapped, unManagedOverlapped, false);
                         *
                         * //var resultReadFile = NativeMethods.ReadFile(Handle, buffer, (uint) buffer.Length, out bytesRead, ref overlapped);
                         *
                         *
                         * var resultReadFile = NativeMethods.ReadFileUnsafe(Handle, unManagedBuffer,(uint) buffer.Length, unManagedBytesRead,  (NativeOverlapped*)unManagedOverlapped);
                         */
                        if (!resultReadFile && Marshal.GetLastWin32Error() == NativeMethods.ERROR_IO_PENDING)
                        {
                            Console.Out.WriteLine("Marshal.GetLastWin32Error() --------------->  " + Marshal.GetLastWin32Error());
                            var result = NativeMethods.WaitForSingleObject(overlapped.EventHandle, overlapTimeout);
                            //Console.Out.WriteLine("******************BBBBB*********************");
                            //Console.Out.WriteLine("NativeMethods.WaitForSingleObject() --------------->  " + result);
                            switch (result)
                            {
                            case NativeMethods.WAIT_OBJECT_0:
                                status = HidDeviceData.ReadStatus.Success;
                                var stringBuilder = new StringBuilder();

                                //stringBuilder.AppendLine("Length = " + buffer.Length);
                                for (int i = 0; i < buffer.Length; i++)
                                {
                                    stringBuilder.AppendLine("[" + i + "] = " + buffer[i] + " ");
                                }
                                Console.Out.WriteLine("Before GetOverlappedResult bytesRead :  " + bytesRead +
                                                      "\n" + stringBuilder.ToString());

                                //var fresult = NativeMethods.GetOverlappedResultUnsafe(Handle, (NativeOverlapped*)unManagedOverlapped, out bytesRead, true);
                                var fresult = NativeMethods.GetOverlappedResult(Handle, ref overlapped, out bytesRead, true);
                                Console.Out.WriteLine("NativeMethods.GetOverlappedResult result is : " + fresult);
                                NativeMethods.ResetEvent(overlapped.EventHandle);
                                stringBuilder.Clear();
                                //stringBuilder.AppendLine("Length = " + buffer.Length);
                                //buffer = (byte[]) unManagedBuffer
                                for (int i = 0; i < buffer.Length; i++)
                                {
                                    stringBuilder.AppendLine("[" + i + "] = " + buffer[i] + " ");
                                }
                                Console.Out.WriteLine("After GetOverlappedResult bytesRead :  " + bytesRead +
                                                      "\n" + stringBuilder.ToString());
                                break;

                            case NativeMethods.WAIT_TIMEOUT:
                                status = HidDeviceData.ReadStatus.WaitTimedOut;
                                buffer = new byte[] {};
                                break;

                            case NativeMethods.WAIT_FAILED:
                                status = HidDeviceData.ReadStatus.WaitFail;
                                buffer = new byte[] {};
                                break;

                            default:
                                status = HidDeviceData.ReadStatus.NoDataRead;
                                buffer = new byte[] {};
                                break;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        status = HidDeviceData.ReadStatus.ReadError;
                        //Console.Out.WriteLine("catch! --------------->  " + ex.Message + "\n" + ex.StackTrace);
                    }
                    finally
                    {
                        CloseDeviceIO(overlapped.EventHandle);
                    }
                }
                else
                {
                    try
                    {
                        var overlapped = new NativeOverlapped();

                        NativeMethods.ReadFile(Handle, buffer, (uint)buffer.Length, out bytesRead, ref overlapped);
                        status = HidDeviceData.ReadStatus.Success;
                    }
                    catch
                    {
                        status = HidDeviceData.ReadStatus.ReadError;
                    }
                }
            }
            return(new HidDeviceData(buffer, status));
        }