Win32 File API

The Win32 File API provides a comprehensive set of functions for interacting with the file system on Windows. This includes operations such as creating, opening, reading, writing, and deleting files, as well as managing directories and file attributes.

Key Concepts

Understanding the following concepts is crucial when working with the Win32 File API:

Common File Operations

CreateFile

Creates or opens a file or I/O device. This is the primary function for gaining access to files.

ReadFile

Reads data from a file or I/O device into a buffer.

WriteFile

Writes data from a buffer to a file or I/O device.

CloseHandle

Closes an open object handle, releasing associated resources.

SetFilePointerEx

Moves the file pointer (current read/write position) of a file.

DeleteFile

Deletes an existing file.

CopyFile

Copies an existing file to a new location.

MoveFile

Moves an existing file or renames a file.

Directory Operations

Manipulating directories is also a common task:

Example: Reading a File

Here's a simplified C++ example demonstrating how to read from a file:


#include <windows.h>
#include <iostream>
#include <vector>

int main() {
    HANDLE hFile = CreateFile(
        L"example.txt",           // File name
        GENERIC_READ,             // Desired access
        FILE_SHARE_READ,          // Share mode
        NULL,                     // Security attributes
        OPEN_EXISTING,            // Creation disposition
        FILE_ATTRIBUTE_NORMAL,    // Flags and attributes
        NULL                      // Template file
    );

    if (hFile == INVALID_HANDLE_VALUE) {
        std::cerr << "Error opening file: " << GetLastError() << std::endl;
        return 1;
    }

    DWORD fileSize = GetFileSize(hFile, NULL);
    if (fileSize == INVALID_FILE_SIZE) {
        std::cerr << "Error getting file size: " << GetLastError() << std::endl;
        CloseHandle(hFile);
        return 1;
    }

    std::vector<char> buffer(fileSize);
    DWORD bytesRead;

    if (ReadFile(hFile, buffer.data(), fileSize, &bytesRead, NULL) && bytesRead == fileSize) {
        std::cout.write(buffer.data(), bytesRead);
    } else {
        std::cerr << "Error reading file: " << GetLastError() << std::endl;
    }

    CloseHandle(hFile);
    return 0;
}
        

API Reference Snippet

CreateFile

Parameter Type Description
lpFileName LPCTSTR The name of the file or device to be created or opened.
dwDesiredAccess DWORD The generic access rights. This parameter can be a combination of the following values.
dwShareMode DWORD Specifies how the file may be shared.
lpSecurityAttributes LPSECURITY_ATTRIBUTES A pointer to a SECURITY_ATTRIBUTES structure that contains the security descriptor for the object.
dwCreationDisposition DWORD Determines the action to take if a file that matches the given name exists or does not exist.
dwFlagsAndAttributes DWORD The file attributes and extended attributes for the file.
hTemplateFile HANDLE A handle to a template file with the attributes and extended attributes to be set for the file being created.

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.