OWL NExt - Knowledge Base

[ Home | Contents | Search | Next | Previous | Up ]

Using File Mapping for Shared Data.

Date: 12/1/99
Time: 10:02:21 PM

Question:

With the introduction of Win32 we can no longer use DLL's to
share memory between proccesses.  I was told to use FileMapping.
What is FileMapping and how do I do this?

Answer:

You use file maping to set segments into shared spaces so that 2
or more different processes have access to the same segment to
share data. Below is an example that walks you though filemapping.

#include <iostream.h>
#include <windows.h>
#include <conio.h>
#pragma hdrstop

//structure that will be shared
typedef struct INFO_TAG {
   int  nThing;
   char sThing[256];
}INFO, *LPINFO;

//globals to store the mapped info
static HANDLE hMemMap;
static HANDLE hFile;
static void *pMapView;

//this function will create the file mapping
bool __stdcall SetupFileMap(void)
{
   bool bExists;

   //create a file to map to
   hFile = CreateFile("temp",
                       GENERIC_READ | GENERIC_WRITE,
                       FILE_SHARE_READ | FILE_SHARE_WRITE,
                       NULL,
                       OPEN_ALWAYS,
                       FILE_ATTRIBUTE_HIDDEN | FILE_FLAG_DELETE_ON_CLOSE,
                       NULL);

   //create the mapping to the file
   hMemMap = CreateFileMapping(hFile,
                               NULL,
                               PAGE_READWRITE,
                               0,
                  sizeof(INFO),
                               "FM_MemMap");

   //see weather this is the first time this file has been mapped to
   bExists = (GetLastError() == ERROR_ALREADY_EXISTS);

   //Map a view into the Mapped file
   pMapView = MapViewOfFile(hMemMap,
                            FILE_MAP_ALL_ACCESS,
                            0,
                            0,
                            sizeof(INFO));

  if (!bExists)
  {
     //if it is the first time this map has been made,
     //set all the members of our struct to NULL
     memset (reinterpret_cast<LPINFO>(pMapView), NULL, sizeof(INFO));
  }

   return true;
}

LPINFO __stdcall GetSharedInfo (void)
{
  //return a pointer to the mapped file
  return (reinterpret_cast<LPINFO>(pMapView));
}

void __stdcall ShutdownFileMap(void)
{
   //clean up after yourself
   UnmapViewOfFile(pMapView);
   CloseHandle(hMemMap);
   CloseHandle(hFile);
}

//test to show how to access the mapped file
int main(int, char**)
{
   LPINFO Map = NULL;
   char cInput = NULL;
SetupFileMap();
   Map = GetSharedInfo();

   cInput = 'z';
   do
   {
      switch(cInput)
      {
        case 'c': cout << "Enter integer: ";
                  cin >> Map->nThing;
                  cout << "Enter string: ";
                  cin >> Map->sThing;
                  cInput = 'z';
                  break;

        case 'd': cout << "Integer = " << Map->nThing << endl;
                  cout << "String = " << Map->sThing << endl;
                  cInput = 'z';
                  break;

        default : cout << "Enter e to exit" << endl;
  cout << "      c to change data in the mapped file" << endl;
            cout << "       d to display the data in the mapped file" << endl;
                  cin >> cInput;
                  break;

      }
   } while(cInput != 'e');
  
   ShutdownFileMap();
   return 0;
}

Last changed: July 14, 2001