Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming / file

Interprocess Communication using System::IO::MemoryMappedFiles in MC++

5.00/5 (1 vote)
19 Jul 2013CPOL2 min read 9.6K  
IPC using memory mapped file in managed VC++

Introduction

Interprocess Communication used to be very popular pre dot net world, when we used to communicate between applications using window message and passing data using various technique like named-pipes, Mail Shots, Memory-Mapped Files, and Window Sockets etc.

However it’s lost its sheen, when people starting embracing Dot-Net framework for application development, there it have their own dot-net classes for IPC mechanism.

In this article I am going to use MemoryMappedFiles using MC++, if you see MemoryMappedFiles namespaces and relevant classes are added in DotNet 4.0, before that we have to rely on Win32 wrapped function for IPC communication using Memory Mapped Files.

Using the code

We start with creating structure, which we use as indexed data-structure in memory mapped file.

C#
public value struct fileNode
{
    int iRollNo;
    int EnglishMarks;
    int MathsMarks;
};
Now, first step is to Open Or Create file on hard disk, which will act memory mapped file, for storing temp data. We going to use MemoryMappedFile::CreateFromFile overloaded static function to create it
C#
MemoryMappedFile ^mappedFile;
static System::String^ fileName = "c:\\Temp\\MemoryMapped.File";
static System::String^ mappedfileName = "MemoryMapped.File";

 mappedFile =MemoryMappedFile::CreateFromFile(fileName,
        FileMode::OpenOrCreate,
        mappedfileName,
        (long)(sizeof(fileNode) *10));
Here  
  • fileName:- actual file in disk          
  • mappedfileName : the actual mapped name, which can be used by other application to open/ communicate using mapped file.  
  • In 4th parameter is to define maximum size of memory mapped file.

Once we are successful in above step, now we get view accessor for random access of memory location, to read or write the data. 

C#
MemoryMappedViewAccessor^  memViewAccessor;
memViewAccessor= mappedFile->CreateViewAccessor();
Now, to write data to MMF, we can use templatized Write function, where we can provide index and data to be written. Here you define index using sizeof(Of Data) * index location, and second parameter
XML
fileNode tmpObject;
tmpObject.iRollNo= 1;
tmpObject.EnglishMarks= 32;
tmpObject.MathsMarks= 33;

memViewAccessor->Write<fileNode>(0,tmpObject);

tmpObject.iRollNo= 2;
tmpObject.EnglishMarks= 42;
tmpObject.MathsMarks= 43;
memViewAccessor->Write<fileNode>(sizeof(tmpObject),tmpObject);

tmpObject.iRollNo= 3;
tmpObject.EnglishMarks= 52;
tmpObject.MathsMarks= 53;
memViewAccessor->Write<fileNode>(sizeof(tmpObject) *2,tmpObject);

Here, we have provided three indexes, with Data off course. For reading data we have to utilize templatized read function, which take index (Position in memory), and out parameter for retrieved value.

Say now if you want to get fileNode object for 2 index location, we will use this code

C#
fileNode tmpObject2;
memViewAccessor->Read<fileNode>(sizeof(tmpObject2),tmpObject2);
MessageBox::Show(FileNodeToString(tmpObject2));

private: System::String^ FileNodeToString(fileNode tmpObject)
{
  return String::Format("RollNo = {0}  English Marks = {1} Maths Marks = {2}",
                       tmpObject.iRollNo, tmpObject.EnglishMarks,tmpObject.MathsMarks);
 }
Now, to open pre-existing memory mapped file for IPC communication, we have to use MemoryMappedFile::OpenExisting static method, which take pre-existing memory mapped filename and access right to open memory right (MemoryMappedFileRights::Read).
C#
MemoryMappedFile ^mappedFile;
mappedFile = MemoryMappedFile::OpenExisting(mappedfileName,MemoryMappedFileRights::Read);
Once above method is successful, since we have opened in read mode, we have to use overloaded function to get Accessor in read mode otherwise exception will thrown for access right issue. Normal method open MMF in read-write mode.
C#
MemoryMappedViewAccessor^  memViewAccessor;
memViewAccessor= mappedFile->CreateViewAccessor(0,0,MemoryMappedFileAccess::Read);
Using memViewAccessor object we can read the values of fileNode structure, as mentioned above.

Points of Interest

Watch for more article in this series!

License

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