赢得CE:创建命名共享内存

时间:2012-04-14 18:35:40

标签: c++ windows-ce memory-mapped-files

我尝试在win CE 6.0上创建命名共享内存,但可能是进程无法保存数据。 我写了两个过程。第一个将文本写入共享内存,第二个读取。第二个显示空消息窗口。

第一个过程:

#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;
}

第二个过程:

#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;
}

运行流程的程序:

#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;
}

1 个答案:

答案 0 :(得分:1)

从MapViewOfFile获取指向共享内存的指针后,两个进程中的代码都应设置同步模式,以便从/向此内存进行读/写操作,以便:

流程1 - P1

  1. 创建命名文件映射
  2. 获取指向内存的指针
  3. 写入内存
  4. 创建名为mutex,
  5. 向P2(使用互斥锁)发信号通知它已写入内存,P2可以读取它。
  6. P1应该等到P2读取共享内存,它可以简单地等待来自第4点的互斥锁。
  7. 流程2 - P2

    1. 创建已命名的互斥锁,但如果它不存在,则返回错误,或等待直到P1创建此互斥锁。
    2. 创建命名文件映射并获取指向其内存的指针
    3. 处理到互斥锁(从1.)获取,P2现在等待直到P1信号(使用WaitForSingleObject)
    4. 当信号到达时,您可以在读取释放互斥锁后读取内存,以便P1可以从第6点开始处理。