Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

A Wrapped Class of Share Memory

3.68/5 (16 votes)
9 Jul 2007CPOL1 min read 1   1.6K  
Share information betwwen processes using Memory-Mapped File

Introduction

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

C++
/** 
* @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 bonyren@163.com 
*/ 
class MMF 
{ 
public: 
MMF(const string& szMMName); 
public: 
virtual ~MMF(void); 
public: 
///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); 
private: 
HANDLE m_hMapFile;//<the handle of memory-mapped file 
string m_szMMName;//<the name of memory-mapped file 
}; 

Class SharedMemory

C++
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 bonyren@163.com 
*/ 
/************************************************************************/ 
protected: 
SharedMemory(); 
private: 
SharedMemory(const SharedMemory& sm) 
{ 
} 
SharedMemory& operator= (const SharedMemory& sm) 
{ 
} 
public: 
virtual ~SharedMemory(void); 
public: 
///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); 
private: 
map<string,MMF*> m_mapMMF;//<the container of MMFs 
}; 

How to Use It?

Process1

C++
int _tmain(int argc, _TCHAR* argv[]) 
{ 
SharedMemory& sm = SharedMemory::Instance(); 
string szValue("bonyren@gmail.com"); 
bool bRet = sm.WriteSharedMemory("1",szValue); 
if(bRet) 
{ 
printf("write value is %s",szValue.c_str()); 
} 
sm.DeleteSharedMemory("1"); 
return 0; 
} 

Process2

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

History

  • 9th July, 2007: Initial post

License

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