CreateFileMapping Function Explained
The CreateFileMapping function is used to create a file mapping object, which maps a file to one or more regions in the virtual address space of a process.
Function prototype:
HANDLE CreateFileMapping(
HANDLE hFile,
LPSECURITY_ATTRIBUTES lpAttributes,
DWORD flProtect,
DWORD dwMaximumSizeHigh,
DWORD dwMaximumSizeLow,
LPCTSTR lpName
);
Explanation of parameters:
- hFile: The file handle associated with the file mapping. Obtain this handle by opening a file using the CreateFile function.
- lpAttributes: a pointer to a SECURITY_ATTRIBUTES structure specifying the security attributes of the file mapping object. It can be NULL.
- flProtect: Specifies the access protection attributes for a file mapping object. Common values include PAGE_READONLY, PAGE_READWRITE, and so on.
- dwMaximumSizeHigh and dwMaximumSizeLow: specify the maximum size of the file mapping object. If it is 0, it means mapping the entire file.
- The lpName parameter specifies the name of the file mapping object. It can be NULL.
Return value:
- Success: Handle to the file mapping object.
- Failure: NULL. You can use the GetLastError function to retrieve error information.
The file mapping object created using the CreateFileMapping function can be mapped to the virtual address space of a process using the MapViewOfFile function, allowing access to the file’s data using pointers.