Kernel-User Base API: Input and Output

This section details the Windows API functions related to handling input and output operations between the kernel and user-mode applications. These functions are fundamental for interacting with hardware devices, files, and other system resources.

ReadFile

BOOL ReadFile( HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped );
Reads data from a file or device into a buffer. This is a synchronous or asynchronous operation.
Parameters:
hFile: A handle to the file or device to be read.
lpBuffer: A pointer to the buffer that receives the data.
nNumberOfBytesToRead: The maximum number of bytes to be read.
lpNumberOfBytesRead: A pointer to a variable that receives the number of bytes actually read.
lpOverlapped: A pointer to an OVERLAPPED structure for asynchronous I/O.
Return Value:
If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.

WriteFile

BOOL WriteFile( HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped );
Writes data from a buffer to a file or device. This is a synchronous or asynchronous operation.
Parameters:
hFile: A handle to the file or device to be written to.
lpBuffer: A pointer to a buffer containing the data to be written.
nNumberOfBytesToWrite: The number of bytes to write.
lpNumberOfBytesWritten: A pointer to a variable that receives the number of bytes actually written.
lpOverlapped: A pointer to an OVERLAPPED structure for asynchronous I/O.
Return Value:
If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.

DeviceIoControl

BOOL DeviceIoControl( HANDLE hDevice, DWORD dwIoControlCode, LPVOID lpInBuffer, DWORD nInBufferSize, LPVOID lpOutBuffer, DWORD nOutBufferSize, LPDWORD lpBytesReturned, LPOVERLAPPED lpOverlapped );
Sends a control code directly to a specified driver to perform the requested operation. This function is used for device-specific operations that are not covered by standard I/O functions.
Parameters:
hDevice: A handle to the device to send the control code to.
dwIoControlCode: The control code for the operation to perform.
lpInBuffer: A pointer to the buffer containing data to be sent to the driver.
nInBufferSize: The size, in bytes, of the buffer pointed to by lpInBuffer.
lpOutBuffer: A pointer to the buffer that receives data from the driver.
nOutBufferSize: The size, in bytes, of the buffer pointed to by lpOutBuffer.
lpBytesReturned: A pointer to a variable that receives the number of bytes actually written into lpOutBuffer.
lpOverlapped: A pointer to an OVERLAPPED structure for asynchronous I/O.
Return Value:
If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.

CreateFile

HANDLE CreateFile( LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile );
Creates or opens a file, directory, or other device. It returns a handle that can be used with other I/O functions.
Parameters:
lpFileName: The name of the file or device.
dwDesiredAccess: The access to the file or device. This can be read, write, or both.
dwShareMode: The way the file or device is shared in other opening requests.
lpSecurityAttributes: Security attributes for the file or device.
dwCreationDisposition: Action to take if the file already exists or does not exist.
dwFlagsAndAttributes: File or device attributes and flags.
hTemplateFile: A handle to a template file with attributes and flags.
Return Value:
If the function succeeds, the return value is an open handle to the specified file or device. If the function fails, the return value is INVALID_HANDLE_VALUE.

Related Topics