This document outlines the core Win32 API functions and concepts related to creating files on Windows operating systems. Understanding these functions is crucial for applications that need to programmatically manage file system resources.
CreateFile FunctionThe CreateFile function is the primary Win32 API for opening or creating files, pipes, serial ports, communications resources, disks, and consoles. When used for file creation, it allows for fine-grained control over access rights, sharing modes, and file attributes.
HANDLE CreateFile(
LPCSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile
);
lpFileName: The name of the file or device to be created.dwDesiredAccess: The requested access to the file. For creation, common values include GENERIC_WRITE or GENERIC_READ | GENERIC_WRITE.dwShareMode: Specifies how the file may be shared by other threads or processes. 0 means no sharing. FILE_SHARE_READ, FILE_SHARE_WRITE, FILE_SHARE_DELETE can be combined.lpSecurityAttributes: Usually NULL for default security.dwCreationDisposition: Crucial for creation. Common values:
CREATE_NEW: Creates a new file. Fails if the file already exists.CREATE_ALWAYS: Creates a new file. If the file exists, it overwrites the file and sets its size to zero.OPEN_ALWAYS: Creates a new file. If the file exists, the system checks to see whether the file is a directory. If it is, the function fails.OPEN_EXISTING: Opens the file. If the file does not exist, the function fails.dwFlagsAndAttributes: Control file attributes and behaviors (e.g., FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_TEMPORARY, FILE_FLAG_DELETE_ON_CLOSE).hTemplateFile: Usually NULL.If the function succeeds, the return value is an open handle to the specified file. If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.
CREATE_ALWAYS or OPEN_ALWAYS, be aware of the potential for overwriting existing data. Always check the return value and use GetLastError to diagnose issues.
WriteFile FunctionAfter creating a file handle with CreateFile, you typically use WriteFile to write data to it.
BOOL WriteFile(
HANDLE hFile,
const void* lpBuffer,
DWORD nNumberOfBytesToWrite,
LPDWORD lpNumberOfBytesWritten,
LPOVERLAPPED lpOverlapped
);
CloseHandle FunctionIt's essential to close the file handle when you are finished with it to release system resources and ensure data is flushed to disk.
BOOL CloseHandle(
HANDLE hObject
);
Here's a simplified C++ example demonstrating the usage of CreateFile to create a new file and WriteFile to write some text into it.
#include <windows.h>
#include <iostream>
#include <string>
int main() {
HANDLE hFile;
DWORD dwBytesWritten;
const char* dataToWrite = "This is some data being written to the new file.\r\n";
std::string fileName = "my_new_file.txt";
hFile = CreateFile(
fileName.c_str(), // File name
GENERIC_WRITE, // Desired access
0, // Share mode: no sharing
NULL, // Security attributes
CREATE_ALWAYS, // Creation disposition: overwrite if exists
FILE_ATTRIBUTE_NORMAL, // File attributes
NULL // Template file
);
if (hFile == INVALID_HANDLE_VALUE) {
std::cerr << "Error creating file: " << GetLastError() << std::endl;
return 1;
}
if (!WriteFile(
hFile, // Handle to file
dataToWrite, // Data to write
(DWORD)strlen(dataToWrite), // Number of bytes to write
&dwBytesWritten, // Bytes written
NULL // Overlapped structure
)
) {
std::cerr << "Error writing to file: " << GetLastError() << std::endl;
CloseHandle(hFile);
return 1;
}
std::cout << "Successfully created and wrote to '" << fileName << "'" << std::endl;
std::cout << dwBytesWritten << " bytes written." << std::endl;
CloseHandle(hFile); // Close the file handle
return 0;
}
<fstream>, which provides a higher-level abstraction over these Win32 APIs.
GetLastError() to retrieve detailed error codes.CreateFileW) and Wide Character strings (LPCWSTR) to properly handle international characters.OVERLAPPED structures with CreateFile and ReadFileEx/WriteFileEx.