Memory-mapped files are one of the most powerful features available to Windows C++ developers. At the center of this mechanism is MapViewOfFile, a function that allows you to treat file contents as if they were part of your program’s memory.
In this blog post, we’ll walk through a complete example and explain every handle involved — what it represents, why it exists, and how it fits into the Windows memory model.
What Exactly Is MapViewOfFile?
Memory-mapped files are a core part of the Windows memory management system. Instead of manually reading file data into buffers, Windows allows you to map a file directly into your process’s virtual memory. The function responsible for this is MapViewOfFile.
In simple terms:
MapViewOfFile lets you treat a file on disk as if it were an array in memory.
Once mapped, you can read (or write) file contents using normal pointer operations — no repeated calls to ReadFile, no manual buffering.
This mechanism is part of the Win32 API and works together with:
- CreateFile
- CreateFileMapping
- UnmapViewOfFile
Why Memory-Mapped Files Exist
Traditional file I/O works like this:
- Request data from the OS
- OS copies file data into a buffer
- Your program reads from that buffer
Memory mapping removes extra copying. The operating system:
- Maps file data into virtual memory
- Loads pages only when accessed (on demand)
- Uses the system cache efficiently
- Allows sharing between processes
This makes memory mapping ideal for:
- Large files
- High-performance systems
- Random file access
- Inter-process communication
How Mapping a File Works
Mapping a file involves three important objects:
- hFile — File Handle
- hMapping — File Mapping Handle
- pView — Mapped Memory Pointer
Let’s walk through each one conceptually.
hFile — The File Handle
We start by calling CreateFile.
HANDLE hFile = CreateFile(...);
What It Represents?
hFile is a handle to a file object managed by the Windows kernel. It does not contain the file data. Instead, it represents:
- A reference to an open file
- Access permissions
- File metadata
- A kernel-managed file object
Think of it as your program’s official permission slip to access the file
Why It’s Needed?
The file handle tells Windows: “I want to work with this file, and here are my access rights.” Without this handle, you cannot create a file mapping.
When It’s Released?
CloseHandle(hFile);
Once closed, the program no longer has access to the file.
hMapping — The File Mapping Handle
We start by calling CreateFileMapping.
HANDLE hMapping = CreateFileMapping(hFile, ...);
What It Represents?
This handle represents a file mapping object, which is a kernel object describing:
- How the file will be mapped
- Protection flags (read-only, read/write, etc.)
- Maximum size of the mapping
Important:
This still does not map the file into memory. Instead, it creates a blueprint for mapping.
Conceptually:
If hFile is the permission slip to the file, hMapping is the architectural plan for how the file will appear in memory.
Why It Exists Separately?
Windows utilizes a structured approach by separating file access into three distinct layers: the file object, the mapping object (configuration), and the view (the actual memory mapping).
This decoupled architecture provides significant flexibility, enabling developers to create multiple mappings with varying protection levels and facilitate seamless shared memory between processes.
Furthermore, this separation offers granular control over advanced memory management tasks.
When It’s Released?
CloseHandle(hMapping);
This removes the mapping object from the system.
pView — The Mapped View Pointer
We start by calling MapViewOfFile.
LPVOID pView = MapViewOfFile(hMapping, ...);
This is where the file actually becomes accessible as memory.
What It Represents?
This is not a handle. It is a pointer to virtual memory inside your process. This is where the magic happens.
When you invoke the MapViewOfFile function, Windows performs a sophisticated memory orchestration.
First, it reserves a specific range of address space within your process. It then creates a direct link between this space and the file's data.
Rather than loading the entire file at once, the OS intelligently loads data pages into physical memory only when they are accessed—a process known as 'on-demand paging.'
Consequently, the file ceases to be a distant object on the disk and begins to behave like a standard in-memory array.
You can now do:
char* data = static_cast<char*>(pView);
std::cout << data[0];
No ReadFile, no buffers — just pointer access.
When It’s Released?
UnmapViewOfFile(pView);
This removes the file from your process’s address space.
How the OS Delivers the Data
Unlike ReadFile, the OS does not immediately load the entire file. Instead, it will do the following actions:
- Accessing memory triggers a page fault
- The OS loads the required page from disk
- The system cache keeps it in memory
- Future accesses are fast
This mechanism is extremely efficient and is one reason memory-mapped files scale well for large datasets.
Cleaning Up Properly
Each object must be released in reverse order:
- UnmapViewOfFile(pView);
- CloseHandle(hMapping);
- CloseHandle(hFile);
Why this order?
It's important to remember that these elements are interconnected. Because the view is tied to the mapping object, and the mapping object is tied to the file handle, they must be released in a specific order. Failing to do so can lead to unexpected crashes or unstable application behavior.
Sharing Memory Between Processes
One powerful feature of file mappings:
If multiple processes open the same-named mapping object, they can share memory.
Instead of mapping a disk file, you can even pass INVALID_HANDLE_VALUE to CreateFileMapping to create shared memory backed by the system paging file.
This is a common IPC (Inter-Process Communication) technique in Win32.
Conclusion
MapViewOfFile is not just a function — it’s a gateway into Windows’ virtual memory system.
The process involves:
- Opening a file (CreateFile)
- Creating a mapping object (CreateFileMapping)
- Mapping a view into memory (MapViewOfFile)
- Accessing file data through a pointer
- Cleaning up with UnmapViewOfFile
While it may feel lower-level compared to C++ standard streams, it provides unmatched control and performance.
If you're building performance-critical Windows applications — such as game engines, database systems, or file-processing tools — understanding memory-mapped files will make you a significantly stronger systems developer.
Reference:
https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-mapviewoffile
https://learn.microsoft.com/en-us/windows/win32/memory/file-mapping
Ready to get started?
Contact IVC for a free consultation and discover how we can help your business grow online.
Contact IVC for a Free Consultation





