The Shared Memory Library (SharedMemoryLib)

Any questions or comments, please
contact Márcio Sarroglia Pinho at

What is it for?

This document is intended to explain how to use the SharedMemoryLib, a C library that provides an easy way to build a common memory page to be shared between different process/programs.
Where is it?

You can download this library from here.

Which compilers can it be used in?

I have tested this library in Visual C++ 6.0. But, as it does not use compiler- dependent code, it should be easy to port it to other MS Windows based compilers.

How do I include it in my project?

The easiest way to include it in your project is to use the "Add files to project" command on the Visual C++ interface, and add the SharedMemoryLib.cpp file.

For every source file (*.c or *.cpp) from which you need to access shared memory, you have to include the library header file, as follows:

#include "SharedMemoryLib.h"


How does it work?

When you are thinking about using shared memory in Windows, you should be aware that:

1. One process/program has to create the shared memory area;
2. All the other processes/programs must open this area before accessing it;
3. The data in the shared memory area is organized as a array of integers;
4. All programs that no longer need to use the shared memory should close their access to it;
5. The process/program that creates this shared area must close this area.

The following sections show how to do the steps listed above.

Creating a shared area

To create a shared memory area use the function

void CreateSharedMemoryArea(void);

This function should be called ONLY by ONE process/program.

Opening a shared area to use

To use the shared area the process/programs must call:

void OpenSharedMemory(void);

This function should be called by any process/program that needs to use the shared area, except the one that creates the area.

Writing in the shared area

To write in a shared area you should use the following:

WriteOnSharedMemory(int Data, int Position);

Where Data is the data you need to write in the array, and Position refers to the array index where you want to put it.

Reading from the shared area

To retrieve an integer stored in a particular position of the shared area you should use the following:

void ReadFromSharedMemory(int *data, int position);

Where Data is a pointer to the variable in which you intend to store the read value and Position is where you want to read the data from.


Here is an example of how to use this library in a simple read/write situation.

// ------------------------------------------------------------
// Creator.cpp
// This program creates a shared area.
// It should run BEFORE the other program.
// ------------------------------------------------------------
#include <stdio.h>

#include "SharedMemoryLib.h"

void main(int argc, char* argv[])
int i;

// creates the area to be shared

printf("Writing data on the shared memory area...\n");
for (i=0; i< 100; i++)
WriteOnSharedMemory(i*2, i); // performs the writing

printf("The writing is done.\n");

printf("Now you can run the other program to read the shared area.\n");
printf("When you want to close the shared area and finish this program...");
printf("Just press a key.\n");

DestroySharedMemoryArea (); //

printf("The End.");


// ------------------------------------------------------------
// User.cpp
// This program uses a shared area
// It should run AFTER the other program.
// ------------------------------------------------------------
#include <stdio.h>

#include "SharedMemoryLib.h"

void main(int argc, char* argv[])
int i, data;

printf("Reading data from the shared memory area...\n");
for (i=0; i< 100; i++) {
ReadFromSharedMemory(&data, i);
printf("Data[%d] = %3d\n", i, data);

printf("The reading is done.\n");

DestroySharedMemoryArea ();

printf("The End.");