public unsafe void WriteAsync(IntPtr sourceAddress, ulong destinationAddress, uint numBytesToWrite, IOCompletionCallback callback, NativeOverlapped *ovNative) // IAsyncResult asyncResult) { // Overlapped ov = new Overlapped(0, 0, IntPtr.Zero, asyncResult); // NativeOverlapped* ovNative = ov.UnsafePack(callback, IntPtr.Zero); ovNative->OffsetLow = unchecked ((int)(destinationAddress & 0xFFFFFFFF)); ovNative->OffsetHigh = unchecked ((int)((destinationAddress >> 32) & 0xFFFFFFFF)); /* Invoking the Native method WriteFile provided by Kernel32.dll * library. Returns false, if request failed or accepted for async * operation. Returns true, if success synchronously. */ bool result = Native32.WriteFile(logHandle, sourceAddress, numBytesToWrite, out uint bytesWritten, ovNative); if (!result) { int error = Marshal.GetLastWin32Error(); /* Just handle the case when it is not ERROR_IO_PENDING * If ERROR_IO_PENDING, then it is accepted for async execution */ if (error != Native32.ERROR_IO_PENDING) { Overlapped.Unpack(ovNative); Overlapped.Free(ovNative); throw new Exception("Error writing to log file: " + error); } } else { //executed synchronously, so process callback callback(0, bytesWritten, ovNative); } }