ReadFile

Reads data from a file or a communications device.

Function

BOOL ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED *lpOverlapped);

Parameters

Return Value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.

If lpOverlapped is not NULL and the read operation is pending, the return value is zero and GetLastError returns ERROR_IO_PENDING.

Remarks

This function is a file system and device-specific function. For more information, see the documentation for the specific file system or device.

A named pipe server process can use the ReadFile function to read messages sent by a client process. The pipe must be created with the FILE_TYPE_PIPE flag.

When reading from a named pipe, the nNumberOfBytesToRead parameter specifies the maximum number of bytes to read. If the message is larger than this value, ReadFile returns false and GetLastError returns ERROR_MORE_DATA. If the message is smaller than this value, ReadFile returns true and reads the entire message.

If the pipe is in message-read mode, ReadFile reads one message at a time. If the pipe is in byte-stream mode, ReadFile reads up to nNumberOfBytesToRead bytes.

If the client process has closed the other end of the pipe, ReadFile returns false and GetLastError returns ERROR_BROKEN_PIPE.

See Also