Rafał
Rafał

Reputation: 45

Win CE: Creating Named Shared Memory

I try to create Named Shared Memory on win CE 6.0 but probably the process does not save the data. I wrote two processes. The 1st writes the text to the shared memory and the 2nd reads. The 2nd show empty message window.

1st process:

#include "stdafx.h"
#include <stdlib.h>

#define BUFFSIZE 256
TCHAR szName[]=TEXT("MyFileMappingObject");
TCHAR szText[]=TEXT("Process write");

int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])
{
HANDLE hMutex;  
HANDLE hMapFile;
LPCTSTR pBuff;
BOOL fFirstApp = TRUE;
int rc;

// Create mutex used to share memory-mapped structure.
hMutex = CreateMutex (NULL, TRUE, TEXT ("MyFileMOWRT"));
rc = GetLastError();
if (rc == ERROR_ALREADY_EXISTS)
    fFirstApp = FALSE;
else if (rc)
{
    _tprintf(TEXT("rc1 (%d).\n"), GetLastError());
    return 0;
}

// Wait here for ownership to ensure that the initialization is done.
// This is necessary since CreateMutex doesn’t wait.
rc = WaitForSingleObject(hMutex, 2000);
if (rc != WAIT_OBJECT_0)
{
    _tprintf(TEXT("rc2 wait (%d).\n"), GetLastError());
    return 0;
}

// Create a file-mapping object.
hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 
                             BUFFSIZE, szName);
if (hMapFile == NULL)
{
    _tprintf(TEXT("Could not create file mapping object (%d).\n"), GetLastError());
    return 1;
}
else
    printf("File mapping object was created\n");

// Map into memory the file-mapping object.
pBuff = (LPTSTR)MapViewOfFile(hMapFile, FILE_MAP_WRITE, 0, 0, BUFFSIZE);
if (pBuff == NULL)
{
    _tprintf(TEXT("Could not map view of file (%d).\n"), GetLastError());
    CloseHandle(hMapFile);

    return 1;
}
else
    printf("Map view of file\n");

CopyMemory((PVOID)pBuff, szText, (_tcslen(szText) * sizeof(TCHAR)));

UnmapViewOfFile(pBuff);

// Release the mutex. We need to release the mutex twice 
// if we owned it when we entered the wait above.   ReleaseMutex(hMutex);
ReleaseMutex(hMutex);
if (fFirstApp)
    ReleaseMutex(hMutex);

CloseHandle(hMapFile);
CloseHandle(hMutex);

return 0;
}

2nd process:

#include "stdafx.h"
#include <stdlib.h>

#define BUFFSIZE 256
TCHAR szName[]=TEXT("MyFileMappingObject");

int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])
{
    HANDLE hMutex;  
HANDLE hMapFile;
LPCTSTR pBuf;
BOOL fFirstApp = TRUE;
int rc;

// Create mutex used to share memory-mapped structure.
hMutex = CreateMutex (NULL, TRUE, TEXT ("MyFileMOWRT"));
rc = GetLastError();
if (rc == ERROR_ALREADY_EXISTS)
    fFirstApp = FALSE;
else if (rc)
{
    _tprintf(TEXT("rc1 (%d).\n"), GetLastError());
    return 0;
}

// Wait here for ownership to ensure that the initialization is done.
// This is necessary since CreateMutex doesn’t wait.
rc = WaitForSingleObject(hMutex, 2000);
if (rc != WAIT_OBJECT_0)
{
    _tprintf(TEXT("rc2 wait (%d).\n"), GetLastError());
    return 0;
}

// Create a file-mapping object.
hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 
                             BUFFSIZE, szName);
if (hMapFile == NULL)
{
    _tprintf(TEXT("Could not create file mapping object (%d).\n"), GetLastError());
    return 1;
}
else
    printf("File mapping object was created\n");

pBuf = (LPTSTR) MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);    
if (pBuf)
{
    MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);
}
else
{
    _tprintf(TEXT("Could not map view of file (%d).\n"), GetLastError());
    CloseHandle(hMapFile);

    return 1;
}

UnmapViewOfFile(pBuf);

// Release the mutex. We need to release the mutex twice 
// if we owned it when we entered the wait above.   ReleaseMutex(hMutex);
ReleaseMutex(hMutex);
if (fFirstApp)
    ReleaseMutex(hMutex);

CloseHandle(hMapFile);
CloseHandle(hMutex);

return 0;
}

Program which runs processes:

#include "stdafx.h"
#include <stdlib.h>

int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])
{
    CreateProcess(TEXT("\\Windows\\Mutex_proces.exe"), NULL, 0,0,0,0,0,0,0,0);
    CreateProcess(TEXT("\\Windows\\Mutex_proces_rd.exe"), NULL, 0,0,0,0,0,0,0,0);

    return 0;
}

Upvotes: 0

Views: 1892

Answers (1)

marcinj
marcinj

Reputation: 49986

After you get pointer to shared memory from MapViewOfFile, your code in both processes should setup synchronized pattern for reading/writing from/to this memory so:

Process 1 - P1

  1. creates named file mapping
  2. gets pointer to memory
  3. writes to memory
  4. create named mutex,
  5. signalize to P2 (using mutex) that it has written memory, and P2 can read it. .
  6. P1 should wait till P2 reads shared memory, it can simply wait on mutex from point 4.

Process 2 - P2

  1. Creates named mutex, but if it does not exists then either returns with error, or waits till P1 creates this mutex.
  2. Create named filemapping and get pointer to its memory
  3. Handle to mutex (from 1.) is aquired, P2 now waits until P1 signals (use WaitForSingleObject)
  4. When signal arrives then you can read memory, after reading release mutex so that P1 can proceed with processing from point 6.

Upvotes: 1

Related Questions