Click here to Skip to main content
Click here to Skip to main content

A Wrapped Class of Share Memory

, 9 Jul 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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

/** 
* @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

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

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

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)

Share

About the Author

Bony Chen
Web Developer
China China
Bony Chen, a senior software engineer, specializes in C++, COM, C#, ASP.net, 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 bonyren@163.com.

Comments and Discussions

 
Generalthx PinmemberMember 458875610-Sep-08 20:08 

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

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

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