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); } } }
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); } }