Esempio 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);
                    }
                }
            }
        }
Esempio n. 2
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);
                }
            }
        }
Esempio n. 3
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);
            }
        }