共享内存实现进程间通讯

HANDLE hMemShare=NULL;
DWORD* pOD=NULL;
__try{ 
  hMemShare=CreateFileMapping((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE, 0, 8, _T("TestMemShare"));
  if(!hMemShare)
    __leave;
  pOD=(DWORD*)MapViewOfFile(hMemShare, FILE_MAP_WRITE, 0, 0, 8);
  if(!pOD)
    __leave;
  DWORD a=123;
  *pOD=a;
...

  SetEvent(g_hEvent1);
  WaitForSingleObject(g_hEvent2, INFINITE);
  if(*pOD==0)
    ret=TRUE;
}__finally{
  if(pOD)
    UnmapViewOfFile(pOD);
  if(hMemShare)
    CloseHandle(hMemShare);
}
====================================================================
HANDLE hMemShare=NULL;
DWORD* pOD=NULL;
__try{
  WaitForSingleObject(g_hEvent1, INFINITE);
  // 接受从exe传来的参数
  hMemShare=OpenFileMapping(FILE_MAP_WRITE, FALSE, L"TestMemShare");
  if(!hMemShare)
    __leave;
  pOD=(DWORD*)MapViewOfFile(hMemShare, FILE_MAP_WRITE, 0, 0, 8);
  if(!pOD)
    __leave;
  //寄存器fs值:
  DWORD g_wowFS=*(pOD+4);
  *pOD=0;//返回给exe的参数。
  SetEvent(g_hEvent2);
}__finally{
  if(pOD)
    UnmapViewOfFile(pOD);
  if(hMemShare)
    CloseHandle(hMemShare);
}

你可能感兴趣的:(File,null,exe,通讯)