SAMPLE: MMFILE - Class for Memory-Mapped Files

Last reviewed: July 2, 1997
Article ID: Q142377
2.00 2.10 2.20 4.00 WINDOWS NT kbprg kbfile kbcode

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC) included with: Microsoft Visual C++, 32-bit Edition, versions 2.x and 4.0

SUMMARY

Memory-mapped files allow a file on disk to be associated with an address space. Once this mapping is done the data in the file can be accessed as if the file was in memory. Memory-mapped files provide a method on Win32 for sharing blocks of memory between processes.

The CMemoryMappedFile class provides a C++ wrapper class for creating a memory-mapped file and using it. The MMFILE.TXT file included with code provides more information about how to use the class.

The following file is available for download from the Microsoft Software Library:

 ~ Mmfile.exe (size: 24541 bytes) 

For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q119591
   TITLE     : How to Obtain Microsoft Support Files from Online Services

MORE INFORMATION

CMemoryMappedFile is derived from CMemFile. It wraps the file mapping APIs to manage the file mapping. A memory-mapped file does not necessarily have to be associated with a disk file.

To create a memory-mapped file without an associated disk file, call CMemoryMappedFile::Open(). The first argument has to be supplied and it can be any string. This memory-mapped file can be accessed by other processes by using this string when they open the file. The other arguments to Open specify the size and protection flags for this file. The default parameters creates a memory-mapped file with a size of 4096 bytes and does not map to a disk file.

Mapping a file on disk into memory involves opening the disk file with the correct access. Then call CMemoryMappedFile::Open() and pass in the handle to the file. The other parameters specify protection flags for the mapping, size of the mapping, and the part of the file to map. The default parameters try to map the complete file into memory.

The code below shows the two ways of creating the memory-mapped file.

REFERENCES

"Advanced Windows" - Jeffrey Richter Win32 SDK Docs

Sample Code

   // Code to open a memory-mapped file
   //
   CMemoryMappedFile SharedFile1;

   // Opens a memory-mapped file without creating a file on disk.
   // This or other processes can access the file by using the
   // name. Default size is 4096 bytes.
   SharedFile1.Open("Test");
   ...
   SharedFile1.Close();

   //Open a file on disk and map it into memory
   CFile file("DiskFile", CFile::modeReadWrite |
              CFile::modeCreate | CFile::shareDenyNone);

   CMemoryMappedFile SharedFile2;
   SharedFile2.Open("MapFile", (HANDLE) file.m_hFile);
   ...
   SharedFile2.Close();
   file.Close();


KBCategory: kbprg kbfile kbcode
KBSubcategory: MFCFileIO kbsample
Additional reference words: 4.00 2.00 2.10 2.20 memory mapped
Keywords : kbsample MFCFileIO kbcode kbfile kbprg
Technology : kbMfc
Version : 2.00 2.10 2.20 4.00
Platform : NT WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.