Solved

How can I tell when a second process has accessed shared memory?

Posted on 2009-04-09
1
333 Views
Last Modified: 2013-12-03
Hello all,

I have a simple c++ program running in windows that creates a file mapping object, etc. to share some memory with a second process. How can I tell when some process has accessed the shared memory? Please see my code.
#define BUF_SIZE	256	
 

#include <stdio.h>

#include <string.h>

#include <sys/types.h>

#include <windows.h>

#include <conio.h>

#include <tchar.h>

#include <memory.h>
 

char szName[] = "Global\\MyFileMappingObject";

char message[] = "This will be shared";
 

int main()

{

	HANDLE hMapFile = CreateFileMapping(

		INVALID_HANDLE_VALUE,

		NULL,

		PAGE_READWRITE,

		0,

		BUF_SIZE,

		szName);
 

	LPCTSTR pBuf = (LPTSTR)MapViewOfFile(

		hMapFile,

		FILE_MAP_ALL_ACCESS,

		0,

		0,

		BUF_SIZE);
 

	CopyMemory((PVOID)pBuf, message, (strlen(message) * sizeof(TCHAR)));
 

	getch();
 

	UnmapViewOfFile(pBuf);

	CloseHandle(hMapFile);

	return 0;

}

Open in new window

0
Comment
Question by:ehensens
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 24107788
You only can tell that for sure when both processes signal these events to each other. See also http://www.codeproject.com/KB/threads/sharedmemipc.aspx ("Interprocess Communication using Shared Memory"). E.g.
#include <sys/types.h>

#include <windows.h>

#include <conio.h>

#include <tchar.h>

#include <memory.h>

 

char szName[] = "Global\\MyFileMappingObject";

char message[] = "This will be shared";

 
 

HANDLE ConnectEvent ( LPCTSTR pszName)

{
 

   HANDLE hEvent   =   CreateEvent (   NULL,

                                       FALSE,

                                       FALSE,

                                       pszName

                                   );
 

   if  (   INVALID_HANDLE_VALUE    ==  hEvent)

       {

           if  (   ERROR_ALREADY_EXISTS    ==  GetLastError    ())

               {

                   hEvent  =   OpenEvent   (   SYNCHRONIZE,

                                               FALSE,

                                               pszName

                                           );
 

                   if  (   INVALID_HANDLE_VALUE    ==  hEvent)

                       {
 

                           return NULL;

                       }

               }

       }
 

   return hEvent;

}
 

int main()

{

        HANDLE hMapFile = CreateFileMapping(

                INVALID_HANDLE_VALUE,

                NULL,

                PAGE_READWRITE,

                0,

                BUF_SIZE,

                szName);

 

        LPCTSTR pBuf = (LPTSTR)MapViewOfFile(

                hMapFile,

                FILE_MAP_ALL_ACCESS,

                0,

                0,

                BUF_SIZE);
 

        HANDLE hEvent = ConnectEvent("Global\\MyFileMappingEvent");

 

        CopyMemory((PVOID)pBuf, message, (strlen(message) * sizeof(TCHAR)));
 

        // Signal to other process that data was set

        SetEvent(hEvent);

 

        getch();

 

        UnmapViewOfFile(pBuf);

        CloseHandle(hMapFile);

        return 0;

}
 

// other process
 

// ...
 

        HANDLE hEvent = ConnectEvent("Global\\MyFileMappingEvent");

 

        // Wait for other process to signal that data has been copied

        WaitForSingleObject(hEvent,INFINITE);
 

        // Process data
 

        // ...

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now