Click here to Skip to main content
13,548,587 members
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 9 Jul 2007
Licenced CPOL

A Wrapped Class of Share Memory

, 9 Jul 2007
Rate this:
Please Sign up or sign in to vote.
Share information betwwen processes using Memory-Mapped File


How do you share information between processes on Windows platform. Think of it this way: two processes both open the same file and both read and write from it, thus sharing the information. The problem is, sometimes it's a pain to do all those fseek()s and stuff to get around. Wouldn't it be easier if you could just map a section of the file to memory, and get a pointer to it? Then you could simply use pointer arithmetic to get (and set) data in the file.

Well, this is exactly what a memory mapped file is. And it's really easy to use, too. A few simple calls, mixed with a few simple rules, and you're mapping like a mad-person.

Here's the quote from MSDN documentation:

"A memory-mapped file, or file mapping, is the result of associating a file's contents with a portion of the virtual address space of a process. It can be used to share a file or memory between two or more processes..."

Microsoft provides simple Windows API functions to implement memory-mapped file, like CreateFileMapping OpenFileMapping MapViewOfFile, but I don't think it is easy to use for a beginner. So I implement a memory-mapped file wrapped class. I hope it can help you.

How Is It Working?

Class MMF

* @author Bony.Chen 
* @description A wrapped class managing file mapping object 
for the specified file.It encapsulates some 
basic operations such as Create,Open,MapViewOfFile. 
* @mail 
class MMF 
MMF(const string& szMMName); 
virtual ~MMF(void); 
///Create memory mapping file 
bool CreateMapFile(); 
///open memory mapping file that has been existing 
bool OpenMapFile(); 
///close the handle of memory mapping file 
void CloseMapFile(); 
///write data to the memory mapping file 
bool WriteData(const char* szData,int nLen); 
///read data from the memory mapping file 
bool ReadData(string& szData); 
HANDLE m_hMapFile;//<the handle of memory-mapped file 
string m_szMMName;//<the name of memory-mapped file 

Class SharedMemory

class SharedMemory 
the class SharedMemory uses the Singleton pattern 
* @author Bony.Chen 
* @description 
the SharedMemory object can contain multiple memory-mapped 
file objects and one MMF object has special name.the object 
is implemented using Singleton pattern. 
* @mail 
SharedMemory(const SharedMemory& sm) 
SharedMemory& operator= (const SharedMemory& sm) 
virtual ~SharedMemory(void); 
///Get the only instance object 
static SharedMemory& Instance(); 
///write data to the special MMF that named 'szName' 
bool WriteSharedMemory(const string& szName,const string& szValue); 
///read data from the special MMF that named 'szName' 
bool ReadSharedMemory(const string& szName,string& szValue); 
///delete the special MMF that named 'szName' 
void DeleteSharedMemory(const string& szName); 
map<string,MMF*> m_mapMMF;//<the container of MMFs 

How to Use It?


int _tmain(int argc, _TCHAR* argv[]) 
SharedMemory& sm = SharedMemory::Instance(); 
string szValue(""); 
bool bRet = sm.WriteSharedMemory("1",szValue); 
printf("write value is %s",szValue.c_str()); 
return 0; 


int _tmain(int argc, _TCHAR* argv[]) 
SharedMemory& sm = SharedMemory::Instance(); 
string szValue; 
bool bRet = sm.ReadSharedMemory("1",szValue); 
printf("read value is %s",szValue.c_str()); 
return 0; 


  • 9th July, 2007: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Bony Chen
Web Developer
China China
Bony Chen, a senior software engineer, specializes in C++, COM, C#,, ISAPI. He has nearly seven years of software development experience, and has successfully developed many influential software products, such as SPES, CSO, QQ (a famous IM tool in China).
And now he is focusing on P2P technology. He aims at being an expert in software development, software consulting, software components and computer science.
If you have any opinions or questions in software area, please contact

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
skyformat99@gmail.com30-Jan-14 15:16
memberskyformat99@gmail.com30-Jan-14 15:16 
QuestionVery nice code, Thank you very much. Pin
fredrick9910-Oct-12 9:04
memberfredrick9910-Oct-12 9:04 
Generaldiuren Pin
laozi8881-Dec-09 21:32
memberlaozi8881-Dec-09 21:32 
GeneralMy vote of 1 Pin
Joe Woodbury28-Jun-09 18:31
memberJoe Woodbury28-Jun-09 18:31 
Generalthx Pin
Member 458875610-Sep-08 19:08
memberMember 458875610-Sep-08 19:08 
QuestionSynchronization? Pin
giaur10-Mar-08 22:48
membergiaur10-Mar-08 22:48 
Generalworship message Pin
stefan.marek29-Aug-07 1:19
memberstefan.marek29-Aug-07 1:19 
Questionmemory leak? Pin
bbrandin24-Jul-07 0:15
memberbbrandin24-Jul-07 0:15 
QuestionCompatibility VC6.0 Pin
luigimoreno23-Jul-07 4:10
memberluigimoreno23-Jul-07 4:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180515.1 | Last Updated 9 Jul 2007
Article Copyright 2007 by Bony Chen
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid