Solved

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

Posted on 2009-04-09
1
352 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

734 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