Thanks japheth.
Similar example with PellesC too:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#pragma data_seg(".shared")
DWORD nCounter = 0;
#pragma data_seg()
#pragma comment(linker,"/SECTION:.shared,RWS")
void __cdecl WinMainCRTStartup(void)
{
TCHAR szMsg[100];
wsprintf(szMsg, TEXT("%d"), nCounter++);
MessageBox(0, szMsg, 0, MB_OK);
wsprintf(szMsg, TEXT("%d"), nCounter);
MessageBox(0, szMsg, 0, MB_OK);
ExitProcess(0);
}
another example of shared memory.#define WIN32_LEAN_AND_MEAN
#include <windows.h>
void *CreateSharedMem(HANDLE *hMap, DWORD nSize)
{
void *pData;
*hMap = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, nSize, "MySharedMem");
if (*hMap == NULL)
return NULL;
pData = MapViewOfFile(*hMap, FILE_MAP_WRITE, 0, 0, 0);
if (pData == NULL)
CloseHandle(*hMap);
return pData;
}
void ReleaseSharedMem(HANDLE *hMap, void *pData)
{
if (!pData) return;
if (pData) UnmapViewOfFile(pData);
if (*hMap) {
CloseHandle(*hMap);
*hMap = NULL;
}
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
HANDLE hMap;
TCHAR szTxt[100];
int *pData = CreateSharedMem(&hMap, 4096);
if (pData) {
wsprintf(szTxt, "%d", *pData);
MessageBox(0, szTxt, 0, MB_OK);
(*pData)++;
wsprintf(szTxt, "%d", *pData);
MessageBox(0, szTxt, 0, MB_OK);
ReleaseSharedMem(&hMap, pData);
}
return 0;
}