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
class MMF
{
public:
MMF(const string& szMMName);
public:
virtual ~MMF(void);
public:
bool CreateMapFile();
bool OpenMapFile();
void CloseMapFile();
bool WriteData(const char* szData,int nLen);
bool ReadData(string& szData);
private:
HANDLE m_hMapFile;string m_szMMName;};
Class SharedMemory
class SharedMemory
{
protected:
SharedMemory();
private:
SharedMemory(const SharedMemory& sm)
{
}
SharedMemory& operator= (const SharedMemory& sm)
{
}
public:
virtual ~SharedMemory(void);
public:
static SharedMemory& Instance();
bool WriteSharedMemory(const string& szName,const string& szValue);
bool ReadSharedMemory(const string& szName,string& szValue);
void DeleteSharedMemory(const string& szName);
private:
map<string,MMF*> m_mapMMF;};
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
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.