简体   繁体   English

Win CE:创建命名共享内存

[英]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 . 我尝试在Win CE 6.0上创建“命名共享内存”,但可能该进程未保存数据 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;
}

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: 从MapViewOfFile获取指向共享内存的指针后,两个进程中的代码都应设置同步模式以从该内存进行读/写操作,以便:

Process 1 - P1 流程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. 向P2(使用互斥锁)发信号通知它已写入内存,并且P2可以读取它。 .
  6. P1 should wait till P2 reads shared memory, it can simply wait on mutex from point 4. P1应该等到P2读取共享内存后,才可以从第4点开始等待互斥量。

Process 2 - P2 工程2-P2

  1. Creates named mutex, but if it does not exists then either returns with error, or waits till P1 creates this mutex. 创建命名的互斥锁,但是如果不存在,则返回错误,或者等到P1创建此互斥锁。
  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) 获取到互斥锁的句柄(从1开始),P2现在等待直到P1发出信号(使用WaitForSingleObject)
  4. When signal arrives then you can read memory, after reading release mutex so that P1 can proceed with processing from point 6. 当信号到达时,您可以在读取释放互斥锁之后读取存储器,以便P1可以从点6开始进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM