2012-04-14 91 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