CMemMapFile LogoCMemMapFile v1.67

Welcome to CMemMapFile, A freeware C++ based class to encapsulate memory mapped files.

 

Features
Usage
Copyright
History
CMemMapFile class members
Contacting the Author

 

 

 

Features

Memory mapping is a powerful mechanism Win32 provides to implement shared memory and also to access files though a simple memory pointer without having to implement your own home brewed buffering mechanisms. As an example its as simple as calling

void* lpData = mmf.Open();
CharUpperBuff(static_cast<LPSTR>(lpData),dwFileLength);

to convert a file to upper case.

Areas where you might find this of interest include very large database files with fixed records, audio processing, string operations and image processing.

The other side of memory mapped files is to implement shared memory. As you will probably know, Win32 puts each process into its own address space, thus making it impossible to pass ordinary pointers across process boundaries. With memory mapped files you get back this very useful mechanism.

This class provides a simple MFC class interface and a simple dialog based application which demonstrates all the functionality of the class.

 

 

Usage

 

 

 

Copyright

 

 

 

History

v1.67 (15 February 2022)

v1.66 (26 March 2020)

v1.65 (16 March 2020)

v1.64 (15 September 2019)

v1.63 (21 April 2019)

v1.62 (1 September 2018)

v1.61 (8 June 2018)

v1.60 (3 June 2018)

v1.59 (18 December 2015)

v1.58 (26 January 2014)

v1.57 (16 March 2012)

v1.56 (15 March 2012)

v1.55 (25 November 2011)

v1.54 (20 November 2011)

v1.53 (6 July 2009)

v1.52 (16 August 2008)

v1.51 (7 July 2006)

v1.50 (2 May 2006)

v1.49 (30 April 2005)

v1.48 (23 December 2004)

v1.47 (6 June 2004)

v1.46 (17 November 2003)

v1.45 (5 October 2003)

v1.44 (4 April 2001)

v1.43 (7 March 2001)

v1.42 (24 March 2000)

v1.41 (21 April 1999)

v1.4 (30 March 1999)

v1.3 (22 October 1998)

v1.2 (29 May 1998)

v1.1 (20 April 1998)

v1.0 (31 March 1998)

 

 

 

CMemMapFile Class Members

CMemMapFile
~CMemMapFile
MapFile
MapMemory
MapExistingMemory
Open
Close
UnMap
UnMapEx
Flush
GetFileHandle
GetFileMappingHandle

 

CMemMapFile::CMemMapFile

CMemMapFile();

Remarks

Standard Constructor for the class.

 

CMemMapFile::~CMemMapFile

~CMemMapFile();

Remarks

Standard destructor for the class. Calls the UnMap function

 

CMemMapFile::MapFile

BOOL MapFile(LPCTSTR pszFilename, BOOL bReadOnly = FALSE, DWORD dwShareMode = 0, LPCTSTR pszMappingName = NULL, LPCTSTR pszMutexName = NULL, BOOL bGrowable = FALSE, const unsigned __int64& dwStartOffset = 0, const SIZE_T& nNumberOfBytesToMap = 0, LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL, BOOL bNoCache = FALSE, LPVOID pBaseAddress = nullptr);

Return Value

TRUE if the file was successfully mapped otherwise FALSE

Parameters

sFilename is the filename to memory map

bReadOnly can be set to true if you are not interested in modifying the file

dwShareMode is the share flags to use when calling CreateFile. This may be prove useful if you want to map a file using MMF techniques but allow other processes access to it as read only

pszMappingName is the Win32 name of the memory mapping you want to create. Normally when you map a file there is no need to do this as the memory mapping is not being used to share memory and you can leave this value as NULL

pszMutexName is the Win32 name of the mutex used to serialize access to the memory mapping

bGrowable If you set this value to TRUE, then the underlying file will be set to be a "sparse" file. In the context of memory mapped files, this means that you will not get access violations when you try to write past the end of the file, instead the OS will silently grow the file for you. Please note that this is only supported on NTFS 5 volumes on Windows 2000 or later. This could be useful where you want to use memory mapped files to implement features such as direct to disk audio recording where you do not want to pre-allocate a certain size disk file up front

dwStartOffset The offset into the file at which you want the file mapping to start

nNumberOfBytesToMap The number of bytes you would like to map. The default value of 0 means that all the file will be mapped

lpSecurityAttributes Allows NT security attributes to be associated with this file mapping

bNoCache If set to TRUE, then the SEC_NOCACHE flag is passed to the CreateFileMapping function

pBaseAddress The base memory address at which to perform the memory mapping

Remarks

Maps a file system file. Note that an attempt to modify a file mapping when it is in read only mode will generate an access violation

 

CMemMapFile::MapMemory

BOOL MapMemory(LPCTSTR pszMappingName, LPCTSTR pszMutexName, const SIZE_T& nBytes, BOOL bReadOnly = FALSE, LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL, BOOL bNoCache = FALSE, const unsigned __int64& dwStartOffset = 0, LPVOID pBaseAddress = nullptr);

Return Value

TRUE if the memory was successfully mapped otherwise FALSE

Parameters

pszMappingName is the Win32 name of the memory mapping you want to create

pszMutexName is the Win32 name of the mutex used to serialize access to the memory mapping

nBytes is the size to map

bReadOnly is as in MapFile

lpSecurityAttributes Allows NT security attributes to be associated with this file mapping

bNoCache If set to TRUE, then the SEC_NOCACHE flag is passed to the CreateFileMapping function

dwStartOffset The offset into the memory at which you want the file mapping to start

pBaseAddress The base memory address at which to perform the memory mapping

Remarks

This creates a piece of shared memory

 

CMemMapFile::MapExistingMemory

BOOL MapExistingMemory(LPCTSTR pszMappingName, LPCTSTR pszMutexName, const SIZE_T& nBytes, BOOL bReadOnly = FALSE, BOOL bInheritHandle = FALSE, LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL, _In_ const unsigned __int64& dwStartOffset = 0, LPVOID pBaseAddress = nullptr);

Return Value

TRUE if the memory was successfully mapped otherwise FALSE

Parameters

pszMappingName is the Win32 name of the memory mapping you want to create

pszMutexName is the Win32 name of the mutex used to serialize access to the memory mapping

nBytes is the size to map

bReadOnly is as in MapFile

bInheritHandle Specifies if the handle is to be inherited by a new process during process creation

lpSecurityAttributes Allows NT security attributes to be associated with this file mapping

dwStartOffset The offset into the memory at which you want the file mapping to start

pBaseAddress The base memory address at which to perform the memory mapping

Remarks

This function is quite similar to MapMemory except that it fails it the mapping is already created. This function can be used to connect to existing shared memory or where you want to detect if this is the first instance of your program to run

 

CMemMapFile::Open

LPVOID Open(DWORD dwTimeout = INFINITE);

Return Value

Pointer to the data as stored in the memory mapped file

Parameters

dwTimeout The dwTimeout value is the time in milliseconds to wait if the mapping is already opened by another thread or process

Remarks

Once you have called MapFile, MapMemory or MapExisitingMemory you can call this function to actually get a pointer to the data. Internally the class uses a mutex to synchronise access to the memory so that you do not have to worry about thread synchronisation problems when using the class. If you use the default value then your thread will be suspended indefinitely if another thread is using the object.

 

CMemMapFile::Close

BOOL Close();

Remarks

This is the corollary of the Open function and there should be a matching call to Close for each call to Open. After calling close you are free to call Open again to get back the pointer

 

CMemMapFile::UnMap

void UnMap();

Remarks

This unmaps the object and releases all the file handles and synchronisation objects. You can consider this function to be the corollary to the Map...() functions. This function is also called in the destructor if you forget to do it yourself

 

CMemMapFile::UnMapEx

void UnMapEx(ULONG UnmapFlags);

Parameters

UnmapFlags The parameter value which is passed to the UnampViewOfFileEx API

Remarks

This unmaps the object and releases all the file handles and synchronisation objects using UnmapViewOfFileEx rather than UnmapViewOfFile.

 

CMemMapFile::Flush

BOOL Flush();

Return Value

Returns TRUE if the flush was successful otherwise FALSE

Remarks

Flushes the mapping object to disk.

 

CMemMapFile::GetFileHandle

HANDLE GetFileHandle()

Return Value

Returns the file handle which this memory mapping instance encapsulates. This value returned will be INVALID_HANDLE_VALUE if it is called on a memory mapping which is being used to share memory rather than a file on the file system.

 

CMemMapFile::GetFileMappingHandle

HANDLE GetFileMappingHandle();

Return Value

Returns the file mapping handle which this memory mapping instance encapsulates.

 

 

 

Contacting the Author

PJ Naughter
Email: pjna@naughter.com
Web: http://www.naughter.com
15 February 2022