Click here to Skip to main content
Licence CPOL
First Posted 16 Apr 2007
Views 26,942
Downloads 646
Bookmarked 21 times

Sharing Data with Memory Mapped Files

By | 14 Nov 2010 | Article
How to use memory mapped files.

Screenshot - img1.gif

Screenshot - img2.gif

Introduction

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

Background

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

m_hFileMMF = CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,4*1024,"MyMMF");
DWORD dwError = GetLastError();
if ( ! m_hFileMMF )
 { MessageBox(_T("Creation of file mapping failed"));
 }
 else
 { 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

  
  UpdateData(TRUE);
  g_mutex.Lock();
  if(m_pViewMMFFile )
	lstrcpy( (LPTSTR) m_pViewMMFFile, m_Text);
  g_mutex.Unlock();

Reading data from memory mapped file

  g_mutex.Lock();
  if(m_pViewMMFFile)
  {
    lstrcpy(sReadText, (LPCTSTR) m_pViewMMFFile);
    m_ReadText = sReadText;
    GetDlgItem(IDC_EDIT2)->SetWindowText(m_ReadText);
  }
  g_mutex.Unlock();

Points of Interest

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

License

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

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 2 PinmemberXTAL25618:52 10 Jan '12  
QuestionHow the... PinmvpDave Kreskowiak17:59 14 Nov '10  
GeneralCheck this too.. PinmemberSreekanth Muralidharan1:17 24 Jun '08  
GeneralA little simplistic PinmemberStuart Dootson21:33 16 Apr '07  
GeneralRe: A little simplistic PinmemberBlake Miller7:52 17 Apr '07  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 14 Nov 2010
Article Copyright 2007 by Kumar Sudhir
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid