I hope you need data that will be shared cross two processes.
See, this thread.
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=921874&SiteID=1
You can declare process shared section
#pragma section(".shared", read, write, shared)
Then use __declspec(allocate(".shared")) to declare shared data.
#define SHARED __declspec(allocate(".shared"))
SHARED volatile LONG g_nInstances = 0; //Cross process shared varible of long tipe
SHARED volatile MSG msg = {0}; //Cross process shared structure (MSG type)
You also, can use WM_COPYDATA message for cross process interaction.
Compile this Win32 console application. Run many instances of this application. See, the output. First you see a shared section.
// ProcessMarshal.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <time.h>
using namespace std;
#pragma section(".shared", read, write, shared)
#define SHARED __declspec(allocate(".shared"))
SHARED volatile //volatile required because a compiler can generate a code, that will store and change data in register.
//thats why you can unpredictable results.
LONG g_nInstances = 0;
SHARED volatile MSG msg = {0};
void CopyData();
int _tmain(int argc, _TCHAR* argv[])
{
InterlockedIncrement(&g_nInstances);// Synchronization because another process can access for
// this field. Interlocked is enough.
cout<<g_nInstances<<endl;
HANDLE hMutex = ::CreateMutex(NULL, FALSE, _T("SyncShared")); //Name required to access the same mutex from different processes.
if(WAIT_FAILED != ::WaitForSingleObject(hMutex, INFINITE))
// Synchronization by mutex because you need update entire structure.
// You cannot use interlocked here, because some field can update one process, some a second.
{
msg.message++;
msg.time += 10;
cout<<"msg.message = "<<msg.message<<endl;
cout<<"msg.time = "<<msg.time<<endl;
ReleaseMutex(hMutex);
}
// Sends a structure vs WM_COPYDATA.
CopyData();
cin.get();
return 0;
}
bool isAny = false;
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
TCHAR name[256];
GetWindowText(hwnd, name, 255);
if(_tcscmp(name, _T("Form1")) == 0)
{
MSG msg = {NULL};
msg.message = g_nInstances;
msg.time = 31;
COPYDATASTRUCT cd = {0};
cd.cbData = sizeof(msg);
cd.dwData = 1;
cd.lpData = &msg;
cout<<"Send Copy Data"<<endl;
SendMessage(hwnd, WM_COPYDATA, NULL, (LPARAM)&cd);
isAny = true;
}
return 1;
}
void CopyData()
{
EnumWindows(&EnumWindowsProc, NULL);
if(!isAny)
cout<<"No windows"<<endl;
}