Kernel32ReadFile(SafeFileHandle, Byte, Int32, Int32, NativeOverlapped) Method

Reads data from the specified file or input/output (I/O) device. Reads occur at the position specified by the file pointer if supported by the device.

This function is designed for both synchronous and asynchronous operations. For a similar function designed solely for asynchronous operation, see ReadFileEx.

Definition

Namespace: DevCase.Win32.NativeMethods
Assembly: DevCase.net48 (in DevCase.net48.dll) Version: 6.0.0.0 (6.0)
XMLNS for XAML: Not mapped to an xmlns.
[DllImportAttribute("Kernel32.dll", ExactSpelling = true, SetLastError = true)]
public static bool ReadFile(
	SafeFileHandle hFile,
	byte[] buffer,
	int numberOfBytesToRead,
	out int refNumberOfBytesRead,
	ref NativeOverlapped refOverlapped
)

Parameters

hFile  SafeFileHandle
A handle to the device (for example, a file, file stream, physical disk, volume, console buffer, tape drive, socket, communications resource, mailslot, or pipe).

The hFile parameter must have been created with read access. For more information, see Generic Access Rights and File Security and Access Rights.

For asynchronous read operations, hFile can be any handle that is opened with the Overlapped flag by the CreateFile(String, FileAccessRights, FileShare, IntPtr, FileMode, CreateFileFlags, IntPtr) function, or a socket handle returned by the socket or accept function.

buffer  Byte
A pointer to the buffer that receives the data read from a file or device.

This buffer must remain valid for the duration of the read operation.

The caller must not use this buffer until the read operation is completed.

numberOfBytesToRead  Int32
The maximum number of bytes to be read.
refNumberOfBytesRead  Int32
A pointer to the variable that receives the number of bytes read when using a synchronous hFile parameter.

ReadFile(SafeFileHandle, Byte, Int32, Int32, NativeOverlapped) sets this value to zero before doing any work or error checking.

Use NULL for this parameter if this is an asynchronous operation to avoid potentially erroneous results.

This parameter can be NULL only when the refOverlapped parameter is not NULL.

refOverlapped  NativeOverlapped
A pointer to an OVERLAPPED structure is required if the hFile parameter was opened with Overlapped, otherwise it can be NULL.

If hFile is opened with Overlapped, the refOverlapped parameter must point to a valid and unique OVERLAPPED structure, otherwise the function can incorrectly report that the read operation is complete.

For an hFile that supports byte offsets, if you use this parameter you must specify a byte offset at which to start reading from the file or device. This offset is specified by setting the Offset and OffsetHigh members of the OVERLAPPED structure.

For an hFile that does not support byte offsets, Offset and OffsetHigh are ignored.

Return Value

Boolean
If the function succeeds, the return value is .

If the function fails, or is completing asynchronously, the return value is .

To get extended error information, call GetLastWin32Error.

Note: The GetLastWin32Error code ERROR_IO_PENDING is not a failure; it means the read operation is pending completion asynchronously.

Remarks

See Also