Ejemplo n.º 1
0
        public unsafe void ReadControl(uint controlCode, byte[] writeBuffer, byte[] readBuffer)
        {
            uint bytesReturned = 0;

            fixed(byte *inBuffer = writeBuffer)
            {
                fixed(byte *outBuffer = readBuffer)
                {
                    var success = false;

                    try
                    {
                        success = InteropKernel32.DeviceIoControl(_readHandle,
                                                                  controlCode,
                                                                  inBuffer,
                                                                  writeBuffer == null ? 0 : (uint)writeBuffer.Length,
                                                                  outBuffer,
                                                                  readBuffer == null ? 0 : (uint)readBuffer.Length,
                                                                  ref bytesReturned,
                                                                  null);
                    }
                    catch (ObjectDisposedException)
                    {
                        throw new Exception("File handle already closed");
                    }

                    if (!success)
                    {
                        HandleIOError(false);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public unsafe void Read(byte[] readBuffer)
        {
            uint bytesRead = 0;

            fixed(byte *outBuffer = readBuffer)
            {
                var success = false;

                try
                {
                    success = InteropKernel32.ReadFile(_readHandle,
                                                       outBuffer,
                                                       (uint)readBuffer.Length,
                                                       ref bytesRead,
                                                       null);
                }
                catch (ObjectDisposedException)
                {
                    throw new Exception("File handle already closed");
                }

                if (!success)
                {
                    HandleIOError(false);
                }
            }
        }
Ejemplo n.º 3
0
        public unsafe void Write(byte[] writeBuffer)
        {
            uint bytesWritten = 0;

            fixed(byte *inBuffer = writeBuffer)
            {
                var success = false;

                try
                {
                    success = InteropKernel32.WriteFile(_writeHandle,
                                                        inBuffer,
                                                        (uint)writeBuffer.Length,
                                                        ref bytesWritten,
                                                        null);
                }
                catch (ObjectDisposedException)
                {
                    throw new Exception("File handle already closed");
                }

                if (!success)
                {
                    HandleIOError(false);
                }
            }
        }
Ejemplo n.º 4
0
        public unsafe void WriteControl(uint controlCode, byte[] writeBuffer)
        {
            uint bytesReturned = 0;

            fixed(byte *inBuffer = writeBuffer)
            {
                var success = false;

                try
                {
                    success = InteropKernel32.DeviceIoControl(_writeHandle,
                                                              controlCode,
                                                              inBuffer,
                                                              writeBuffer == null ? 0 : (uint)writeBuffer.Length,
                                                              null,
                                                              0,
                                                              ref bytesReturned,
                                                              null);
                }
                catch (ObjectDisposedException)
                {
                    throw new Exception("File handle already closed");
                }

                // retrieve the error on failures
                if (!success)
                {
                    HandleIOError(false);
                }
            }
        }
Ejemplo n.º 5
0
        private void OpenSeparateHandles()
        {
            // open a handle for writing
            _writeHandle = InteropKernel32.CreateFile(_devicePath,
                                                      (uint)InteropKernel32.AccessRights.GENERIC_WRITE,
                                                      (uint)InteropKernel32.ShareModes.FILE_SHARE_WRITE |
                                                      (uint)InteropKernel32.ShareModes.FILE_SHARE_READ,
                                                      IntPtr.Zero,
                                                      (uint)InteropKernel32.CreationDispositions.OPEN_EXISTING,
                                                      (uint)InteropKernel32.FileAttributeFlags.FILE_FLAG_OVERLAPPED,
                                                      IntPtr.Zero);

            // open a handle for reading
            _readHandle = InteropKernel32.CreateFile(_devicePath,
                                                     (uint)InteropKernel32.AccessRights.GENERIC_READ,
                                                     (uint)InteropKernel32.ShareModes.FILE_SHARE_WRITE |
                                                     (uint)InteropKernel32.ShareModes.FILE_SHARE_READ,
                                                     IntPtr.Zero,
                                                     (uint)InteropKernel32.CreationDispositions.OPEN_EXISTING,
                                                     (uint)InteropKernel32.FileAttributeFlags.FILE_FLAG_OVERLAPPED,
                                                     IntPtr.Zero);

            if (_writeHandle == null || _readHandle == null || _writeHandle.IsInvalid || _readHandle.IsInvalid)
            {
                CleanupHandles();

                throw new Exception("Oh noes Cap'n, something is wrong with the handles!");
            }
        }
Ejemplo n.º 6
0
 private void CleanupHandle(SafeFileHandle handle)
 {
     if (handle != null && !handle.IsClosed)
     {
         InteropKernel32.CloseHandle(handle);
         handle.Close();
     }
 }
Ejemplo n.º 7
0
 private void CancelOverlapped(SafeFileHandle handle, ManualResetEvent completedEvent)
 {
     lock (_cancelIOLock)
     {
         if (!completedEvent.WaitOne(OVERLAPPED_TIMEOUT) && handle != null && !handle.IsClosed && !handle.IsInvalid)
         {
             InteropKernel32.CancelIo(handle);
         }
     }
 }
Ejemplo n.º 8
0
        public void WriteClear(uint controlCode)
        {
            // cancel pending operations
            lock (_cancelIOLock)
            {
                InteropKernel32.CancelIo(_readHandle);
                InteropKernel32.CancelIo(_writeHandle);
            }

            WriteControl(controlCode, null);
        }
Ejemplo n.º 9
0
        public unsafe void ReadControlOverlapped(uint controlCode, byte[] writeBuffer, byte[] readBuffer)
        {
            var  completedEvent = new ManualResetEvent(false);
            uint bytesReturned  = 0;
            var  inOverlapped   = new Overlapped();

            inOverlapped.EventHandleIntPtr = completedEvent.SafeWaitHandle.DangerousGetHandle();
            NativeOverlapped *inNativeOverlapped = inOverlapped.Pack(null, null);

            try
            {
                fixed(byte *inBuffer = writeBuffer)
                {
                    fixed(byte *outBuffer = readBuffer)
                    {
                        var success = false;

                        try
                        {
                            success = InteropKernel32.DeviceIoControl(_readHandle,
                                                                      controlCode,
                                                                      inBuffer,
                                                                      writeBuffer == null ? 0 : (uint)writeBuffer.Length,
                                                                      outBuffer,
                                                                      readBuffer == null ? 0 : (uint)readBuffer.Length,
                                                                      ref bytesReturned,
                                                                      inNativeOverlapped);
                        }
                        catch (ObjectDisposedException)
                        {
                            throw new Exception("File handle already closed");
                        }

                        if (!success)
                        {
                            HandleIOError(true);

                            CancelOverlapped(_readHandle, completedEvent);
                        }
                    }
                }
            }
            finally
            {
                Overlapped.Free(inNativeOverlapped);
            }
        }
Ejemplo n.º 10
0
        public unsafe void ReadOverlapped(byte[] readBuffer)
        {
            var  completedEvent = new ManualResetEvent(false);
            uint bytesRead      = 0;
            var  inOverlapped   = new Overlapped();

            inOverlapped.EventHandleIntPtr = completedEvent.SafeWaitHandle.DangerousGetHandle();
            NativeOverlapped *inNativeOverlapped = inOverlapped.Pack(null, null);

            try
            {
                // send the data to the device
                fixed(byte *outBuffer = readBuffer)
                {
                    var success = false;

                    try
                    {
                        success = InteropKernel32.ReadFile(_readHandle,
                                                           outBuffer,
                                                           (uint)readBuffer.Length,
                                                           ref bytesRead,
                                                           inNativeOverlapped);
                    }
                    catch (ObjectDisposedException)
                    {
                        throw new Exception("File handle already closed");
                    }

                    if (!success)
                    {
                        HandleIOError(true);

                        CancelOverlapped(_readHandle, completedEvent);
                    }
                }
            }
            finally
            {
                // clean up
                Overlapped.Free(inNativeOverlapped);
            }
        }
Ejemplo n.º 11
0
        public unsafe void WriteOverlapped(byte[] writeBuffer)
        {
            var  completedEvent = new ManualResetEvent(false);
            uint bytesWritten   = 0;
            var  outOverlapped  = new Overlapped();

            outOverlapped.EventHandleIntPtr = completedEvent.SafeWaitHandle.DangerousGetHandle();
            NativeOverlapped *outNativeOverlapped = outOverlapped.Pack(null, null);

            try
            {
                // send the data to the device
                fixed(byte *inBuffer = writeBuffer)
                {
                    var success = false;

                    try
                    {
                        success = InteropKernel32.WriteFile(_writeHandle,
                                                            inBuffer,
                                                            (uint)writeBuffer.Length,
                                                            ref bytesWritten,
                                                            outNativeOverlapped);
                    }
                    catch (ObjectDisposedException)
                    {
                        throw new Exception("File handle already closed");
                    }

                    if (!success)
                    {
                        HandleIOError(true);

                        CancelOverlapped(_writeHandle, completedEvent);
                    }
                }
            }
            finally
            {
                Overlapped.Free(outNativeOverlapped);
            }
        }
Ejemplo n.º 12
0
        private void OpenSingleHandle()
        {
            _handle = InteropKernel32.CreateFile(_devicePath,
                                                 (uint)InteropKernel32.AccessRights.GENERIC_READ |
                                                 (uint)InteropKernel32.AccessRights.GENERIC_WRITE,
                                                 (uint)InteropKernel32.ShareModes.FILE_SHARE_WRITE |
                                                 (uint)InteropKernel32.ShareModes.FILE_SHARE_READ,
                                                 IntPtr.Zero,
                                                 (uint)InteropKernel32.CreationDispositions.OPEN_EXISTING,
                                                 (uint)InteropKernel32.FileAttributeFlags.FILE_FLAG_OVERLAPPED |
                                                 (uint)InteropKernel32.FileAttributeFlags.FILE_FLAG_NO_BUFFERING,
                                                 IntPtr.Zero);

            if (_handle == null || _handle.IsInvalid)
            {
                CleanupHandles();

                throw new Exception("Oh noes Cap'n, something is wrong with the handles!");
            }

            // use this handle for both reads and writes
            _readHandle  = _handle;
            _writeHandle = _handle;
        }