c++ - 赢得 CE : Creating Named Shared Memory

标签 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;
}

最佳答案

从 MapViewOfFile 获得指向共享内存的指针后,您在两个进程中的代码应该设置同步模式以从/写入此内存,因此:

进程 1 - P1

  1. 创建命名文件映射
  2. 获取内存指针
  3. 写入内存
  4. 创建命名互斥锁,
  5. 向 P2 发出信号(使用互斥量)它已写入内存,P2 可以读取它。 .
  6. P1 应该等到 P2 读取共享内存,它可以简单地从第 4 点开始等待互斥量。

进程 2 - P2

  1. 创建命名的互斥量,但如果它不存在则返回错误,或者等到 P1 创建此互斥量。
  2. 创建命名文件映射并获取指向其内存的指针
  3. 获取互斥量(从 1.)的句柄,P2 现在等待直到 P1 发出信号(使用 WaitForSingleObject)
  4. 当信号到来时就可以读内存了,读完后释放互斥锁,这样P1就可以从第6点开始继续处理了。

关于c++ - 赢得 CE : Creating Named Shared Memory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10156231/

相关文章:

c++ - 在 Windows 下获取指向调用堆栈底部的指针并通过地址(如 dladdr)解析符号?

compact-framework - Windows CE 在重置时删除 .NET CF

windows-ce - Windows CE 上的 isatty

java - 当使用 StandardOpenOption.SYNC 打开底层 channel 时,我们是否必须刷新 MappedByteBuffer

c - 映射两个连续的页面

java内存映射文件多线程读/写

c++ - IMFSourceReader M4A 音频精确帧搜索

c++ - 当返回一个没有 RVO 的类/结构时会发生什么?

c++ - 关于在 TCP 缓冲区中序列化 vector<int32_t>?

usb - 是否可以在 Window CE 6.0 仿真器中使用主机 PC 的 USB 端口?