Click here to Skip to main content
13,257,667 members (53,334 online)
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 16 Apr 2007

Sharing Data with Memory Mapped Files

, 14 Nov 2010
Rate this:
Please Sign up or sign in to vote.
How to use memory mapped files.

Screenshot - img1.gif

Screenshot - img2.gif


This article shows how two processes can share data with each other with the help of memory mapped files. The example was written using VC++ 6.0


I wanted a flexible way to share data in between two different processes. Memory mapped files have very important role in Windows, as well as in some other operating systems.

Using the code

In this sample application, one process will write some data to a memory mapped file and other processes will then read that data. Here I am using two different instances of the same application to both read from and write data to a memory mapped file.

Creating the memory mapped file and mapping it to memory

DWORD dwError = GetLastError();
if ( ! m_hFileMMF )
 { MessageBox(_T("Creation of file mapping failed"));
 { m_pViewMMFFile = MapViewOfFile(m_hFileMMF,FILE_MAP_ALL_ACCESS,0,0,0); // map all file
    if(! m_pViewMMFFile )
    { MessageBox(_T("MapViewOfFile function failed"));

Writing data to memory mapped file

if(m_pViewMMFFile )
  lstrcpy( (LPTSTR) m_pViewMMFFile, m_Text);

Reading data from memory mapped file

  lstrcpy(sReadText, (LPCTSTR) m_pViewMMFFile);
  m_ReadText = sReadText;

Points of Interest

I think memory mapped files can solve many problems very easily but people don't use them very often.


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


About the Author

Kumar Sudhir
India India
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 2 Pin
XTAL25610-Jan-12 19:52
memberXTAL25610-Jan-12 19:52 
QuestionHow the... Pin
Dave Kreskowiak14-Nov-10 18:59
mvpDave Kreskowiak14-Nov-10 18:59 
GeneralCheck this too.. Pin
Sreekanth Muralidharan24-Jun-08 2:17
memberSreekanth Muralidharan24-Jun-08 2:17 
GeneralA little simplistic Pin
Stuart Dootson16-Apr-07 22:33
memberStuart Dootson16-Apr-07 22:33 
Given that your main driver is talking about cross-process comms, you ought to touch on the need for cross-process synchronisation to ensure that the reader and writer see coherent views of the memory mapped file. So, either use a mutex to lock the file when accessing it, or a pair of events to indicate when the buffer is ready for reading/writing.

GeneralRe: A little simplistic Pin
Blake Miller17-Apr-07 8:52
memberBlake Miller17-Apr-07 8:52 

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.171114.1 | Last Updated 14 Nov 2010
Article Copyright 2007 by Kumar Sudhir
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid