Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

ReadFile (kernel32)
 
.
Summary
Summary

C# Signature:

[DllImport("kernel32.dll")]
static extern bool ReadFileScatter(IntPtr hFile, FILE_SEGMENT_ELEMENT []
   aSegementArray, uint nNumberOfBytesToRead, IntPtr lpReserved,
   [In] ref System.Threading.NativeOverlapped lpOverlapped);

or

//1
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool ReadFile(IntPtr hFile, [Out] byte[] lpBuffer,
   uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, IntPtr lpOverlapped);

[DllImport("kernel32.dll", SetLastError=true)]
static extern unsafe int ReadFileScatter(IntPtr hFile,
  FILE_SEGMENT_ELEMENT* aSegmentArray, int nNumberOfBytesToRead,
  IntPtr lpReserved, NativeOverlapped* lpOverlapped);

or

User-Defined Types:

[StructLayout(LayoutKind.Explicit, Size = 8)]
internal struct FILE_SEGMENT_ELEMENT
{
  [FieldOffset(0)]
  public IntPtr Buffer;
  [FieldOffset(0)]
  public UInt64 Alignment;
}
//2
[DllImport("kernel32.dll", SetLastError=true)]
static extern unsafe int ReadFile(IntPtr handle, IntPtr bytes, uint numBytesToRead,
  IntPtr numBytesRead, NativeOverlapped* overlapped);

Notes:

The documentation for the Scatter/Gather functions states that the buffer addresses used must be page aligned (not just storage aligned). Indeed, passing a managed allocation, such as a byte array, causes error 87 (Invalid Parameter). This function is asynchronous only so an use the AsyncResult and Overlapped pattern established by the FileStream.

or

Tips & Tricks:

It is recommended that VirtualAlloc be used to allocate an unmanaged block of memory for use with this function. The array of file segments can be effeciently constructed without heap allocation using stackalloc. You will need to get the system's page size through a call to GetSystemInfo.

//3
[DllImport("kernel32.dll", SetLastError=true)]
static extern bool ReadFile(IntPtr hFile, [Out] byte[] lpBuffer, uint nNumberOfBytesToRead,
   out uint lpNumberOfBytesRead, [In] ref System.Threading.NativeOverlapped lpOverlapped);

Sample Code:

// Prepares the array of file segments given an IntPtr[] ABuffers (only uses ACount of the buffers so that these arrays can be pooled)
// The size of each buffer (ASize) must be a multiple of the system's page size (get using GetSystemInfo)
int LPerBufferPageCount = ASize / FPageSize;
FILE_SEGMENT_ELEMENT* LElements = stackalloc FILE_SEGMENT_ELEMENT[(ACount * LPerBufferPageCount) + 1];
for (int LElementIndex = 0; LElementIndex < ACount; LElementIndex++)
{
  for (int LPageIndex = 0; LPageIndex < LPerBufferPageCount; LPageIndex++)
   LElements[(LElementIndex * LPerBufferPageCount) + LPageIndex].Buffer =
    (IntPtr)((uint)ABuffers[LElementIndex] + (LPageIndex * FPageSize));
}
LElements[(ACount * LPerBufferPageCount)].Buffer = IntPtr.Zero;

or

Alternative Managed API:

No managed API as of the 1.1 Framework.

//4
[DllImport(@"kernel32.dll", SetLastError = true)]
static extern unsafe bool ReadFile(
    SafeFileHandle hFile,      // handle to file
    byte* pBuffer,        // data buffer, should be fixed
    int NumberOfBytesToRead,  // number of bytes to read
    IntPtr pNumberOfBytesRead,  // number of bytes read, provide IntPtr.Zero here
    NativeOverlapped *lpOverlapped // should be fixed, if not IntPtr.Zero
);

See Also

WriteFileGather

VB Signature:

<DllImport("kernel32.dll", SetlastError:=True)> _
  Private Shared Function ReadFile(ByVal hFile As IntPtr, ByVal Buffer As IntPtr, _
    ByVal nNumberOfBytesToRead As Integer, ByRef lpNumberOfBytesRead As Integer, _
    ByRef lpOverlapped As OVERLAPPED) As Integer
  End Function

Documentation

or

<DllImport("kernel32.dll")> Friend Shared Function ReadFile( _
    ByVal File As SafeFileHandle, _
    ByVal Buffer As System.Text.StringBuilder, _
    ByVal NumberOfBytesToRead As Integer, _
    ByRef NumberOfBytesRead As Integer, _
    ByRef Overlapped As System.Threading.NativeOverlapped) As SafeFileHandle
End Function

User-Defined Types:

None.

Notes:

The documentation states that if using unbuffered IO, the memory must be "storage aligned" (aligned to the sector size of the storage device). Either this is not enforced, or managed allocations (specifically byte arrays) are automatically storage aligned because there seems to be no problem using it. Note that storage aligned and page aligned are not the same, and managed allocations are not in general page aligned (required for ReadFileScatter).

For more information on unbuffered IO in .NET see:

http://arxiv.org/abs/cs.PF/0502012

Notes:

Regarding the above note, my experience shows that failing to provide a storage-aligned buffer causes ReadFile to perform all IO synchronously. I.e. it always blocks until the operation is complete and never returns ERROR_IO_PENDING.

Warning! GetOverlappedResult writes to the address of the buffer specified in the ORIGINAL OPERATION (ie/ ReadFile or WriteFile). .NET may move the address of the buffer before GetOverlappedResult returns, resulting in a buffer overflow. Use AllocHGlobal and FreeHGlobal or otherwise ensure the buffer is pinned between the two calls. The same applies to the overlapped structure.

Tips & Tricks:

NET 2.0 use SafeFileHandle instead of IntPtr

Warning
using IntPtr to pass in a zero leaks memory. Use the NativeOverlapped versions and pass in a null.

Sample Code:

  Public Function Read() As stArcnetPacket
    Dim dwErrorCode As FarcConstants
    Dim erg As stArcnetPacket
    Dim dwBytesReceived As Int32
    Dim ip As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(erg))
    If ReadFile(hDriver, ip, Marshal.SizeOf(erg), dwBytesReceived, Nothing) Then
      erg = Marshal.PtrToStructure(ip, erg.GetType)
      ReDim Preserve erg.Data(dwBytesReceived - 6)
      Marshal.FreeHGlobal(ip)
    Else
      dwErrorCode = Marshal.GetLastWin32Error
      Marshal.FreeHGlobal(ip)
      Throw New System.IO.IOException("Read fails. Errorcode: " & _
    dwErrorCode.ToString, New System.ComponentModel.Win32Exception(dwErrorCode))
    End If
    Return erg
  End Function

USED W/ SIGNATURE #4

        // This is taken from the USBSharp.cs class
        public unsafe byte[] CT_ReadFile(int InputReportByteLength)
        {
            int BytesRead =0;
            byte[] BufBytes = new byte[InputReportByteLength];
            if (ReadFile(HidHandle, BufBytes, InputReportByteLength, ref BytesRead, null))
            {
                byte[] OutBytes = new byte[BytesRead];
                Array.Copy(BufBytes, OutBytes, BytesRead);
                return OutBytes;
            }
            else
            {
                return null;
            }
        }

Alternative Managed API:

System.IO.FileStream

See Also

WriteFile, WriteFileGather, ReadFileScatter

Documentation
ReadFile on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions