2012-04-14 86 views
0

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

第一进程:

#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

后你的指针从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
+0

我加了互斥的同步点与加工前进,但仍然没有工作.. – 2012-04-21 15:41:16

+0

CopyMemory的(...)在第一进程应等待第二个过程后 - 给它在关闭它之前阅读共享内存。为了测试你可以添加睡眠(100000),只是为了看看它是否有帮助。 [我更新了我的答案] – marcinj 2012-04-22 18:54:15

+0

你是对的,非常感谢!虽然我想到了第一个过程是否过早结束的问题,但我不知道为什么我没有检查过。 – 2012-04-25 10:35:54