public static int WriteFile(UIntPtr hFile, byte[] buffer, uint bytesToWrite, uint offset, out uint numberBytesWritten) { GCHandle hBuffer = GCHandle.Alloc(buffer, GCHandleType.Pinned); int ret = m_fileSystemIo.WriteFile(hFile, (UIntPtr)((uint)hBuffer.AddrOfPinnedObject() + offset), bytesToWrite, out numberBytesWritten); hBuffer.Free(); return(ret); }
public static int ReadFile(IntPtr hfile, byte[] buffer, int nNumberOfBytesToRead, int offset, out int lpNumberOfBytesRead) { GCHandle bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned); int ret = m_fileSystemIo.ReadFile(hfile, new IntPtr(bufferHandle.AddrOfPinnedObject().ToInt32() + offset), nNumberOfBytesToRead, out lpNumberOfBytesRead); bufferHandle.Free(); if (ret != 0) { if (ret == ERROR_ACCESS_DENIED) { throw new UnauthorizedAccessException("Access denied"); } } return(ret); }