File Kernel Object
The File kernel object is a fundamental concept in Windows for interacting with the file system. It represents an open instance of a file or device and provides a handle through which applications can perform operations like reading, writing, seeking, and querying file attributes.
Core Concepts
- Handles: A file object is accessed through a handle obtained via functions like
CreateFile. This handle is a unique identifier used in subsequent API calls. - Access Rights: When creating or opening a file, applications specify desired access rights (e.g.,
GENERIC_READ,GENERIC_WRITE) to control what operations are permitted. - Sharing Modes: Applications can define how the file can be shared with other processes when opening it, using sharing modes like
FILE_SHARE_READandFILE_SHARE_WRITE. - File Attributes: Files can have various attributes (e.g.,
FILE_ATTRIBUTE_READONLY,FILE_ATTRIBUTE_ARCHIVE) that describe their characteristics. - Asynchronous I/O: Windows supports asynchronous file operations, allowing applications to initiate I/O requests and continue processing without waiting for completion, improving performance for I/O-bound tasks.
Key Functions
| Function | Description |
|---|---|
CreateFile |
Opens an existing file or creates a new one. This is the primary function for obtaining a file handle. |
ReadFile |
Reads data from a file into a buffer. |
WriteFile |
Writes data from a buffer to a file. |
SetFilePointer |
Moves the file pointer (the current read/write position) to a specified location within the file. |
GetFileSizeEx |
Retrieves the size of a file. |
CloseHandle |
Closes an open handle to a file object. Essential for releasing system resources. |
DeviceIoControl |
Sends control codes directly to a specified device driver, allowing for device-specific operations beyond standard file I/O. |
CreateFile
HANDLE CreateFile(
[in] LPCTSTR lpFileName,
[in] DWORD dwDesiredAccess,
[in] DWORD dwShareMode,
[in, optional] LPSECURITY_ATTRIBUTES lpSecurityAttributes,
[in] DWORD dwCreationDisposition,
[in] DWORD dwFlagsAndAttributes,
[in, optional] HANDLE hTemplateFile
);
This function is the cornerstone for file operations. It can create, open, truncate, or replace a file. Key parameters include:
lpFileName: The name of the file.dwDesiredAccess: The requested access to the file (e.g.,GENERIC_READ,GENERIC_WRITE).dwShareMode: How the file can be shared with other processes.dwCreationDisposition: Action to take if the file exists or doesn't exist (e.g.,OPEN_EXISTING,CREATE_ALWAYS).dwFlagsAndAttributes: File attributes and flags (e.g.,FILE_ATTRIBUTE_NORMAL,FILE_FLAG_OVERLAPPEDfor asynchronous I/O).
ReadFile
BOOL ReadFile(
[in] HANDLE hFile,
[out] LPVOID lpBuffer,
[in] DWORD nNumberOfBytesToRead,
[out, optional] LPDWORD lpNumberOfBytesRead,
[in, out, optional] LPOVERLAPPED lpOverlapped
);
Reads data from the file specified by hFile into the buffer pointed to by lpBuffer. The number of bytes read is stored in lpNumberOfBytesRead.
WriteFile
BOOL WriteFile(
[in] HANDLE hFile,
[in] LPCVOID lpBuffer,
[in] DWORD nNumberOfBytesToWrite,
[out, optional] LPDWORD lpNumberOfBytesWritten,
[in, out, optional] LPOVERLAPPED lpOverlapped
);
Writes data from the buffer pointed to by lpBuffer to the file specified by hFile. The number of bytes written is stored in lpNumberOfBytesWritten.
SetFilePointer
LONG SetFilePointer(
[in] HANDLE hFile,
[in] LONG lDistanceToMove,
[in, out] PLONG lpDistanceToMoveHigh,
[in] DWORD dwMoveMethod
);
Repositions the file pointer to a specified location within a file. dwMoveMethod can be FILE_BEGIN, FILE_CURRENT, or FILE_END.
GetFileSizeEx
BOOL GetFileSizeEx(
[in] HANDLE hFile,
[out] PLARGE_INTEGER lpFileSize
);
A more modern way to get the file size, returning it as a LARGE_INTEGER (64-bit value), which is essential for large files.
CloseHandle
BOOL CloseHandle(
[in] HANDLE hObject
);
Closes a handle to a kernel object, including file handles. Releasing handles prevents resource leaks.
DeviceIoControl
BOOL DeviceIoControl(
[in] HANDLE hcDevice,
[in] DWORD dwIoControlCode,
[in, optional] LPVOID lpInBuffer,
[in] DWORD nInBufferSize,
[out, optional] LPVOID lpOutBuffer,
[in] DWORD nOutBufferSize,
[out, optional] LPDWORD lpBytesReturned,
[in, out, optional] LPVOID lpOverlapped
);
Used for performing device-specific operations. Many file system operations or driver interactions are exposed through this function using custom IOCTL codes.
GetLastError() to diagnose errors. Properly closing handles is crucial for system stability.
FILE_FLAG_OVERLAPPED flag and functions like ReadFileEx and WriteFileEx.