进程间的通信

通过内存映射文件实现进程间的通信:

 进程间通信 :

接收端:

1.定义句柄:

 HANDLE m_hNVMExit;
 HANDLE m_hNVMEvent;

 

2.初始化:

 m_hNVMExit = NULL;
 m_hNVMEvent = NULL;

 

3.创建线程侦测:

 m_hNVMEvent = CreateEvent(NULL,FALSE,FALSE,_T("TEST"));
 m_hNVMExit  = CreateEvent(NULL,FALSE,FALSE,NULL);

 SetEvent(m_hNVMEvent);
 Sleep(5);
 CreateThread(NULL,0,&GetData,this,0,NULL);

 

DWORD WINAPI GetData(LPVOID pParam)
{
    CTestDlg* pTestDlg = (CTestDlg*)pParam;
 if (pTestDlg == NULL)
  return FALSE;
 
 HANDLE ArrEvent[2] = {pTestDlg ->m_hNVMExit,pTestDlg ->m_hNVMEvent};
 
 while(TRUE)
 {
  DWORD dwRet = WaitForMultipleObjects(2,ArrEvent,FALSE,10);
  if(dwRet == WAIT_OBJECT_0)
   break;
  else if(dwRet == (WAIT_OBJECT_0 + 1))
  {
   HANDLE hMapMem = ::OpenFileMapping(FILE_MAP_ALL_ACCESS,TRUE,_T("CS_DATA"));
   if(hMapMem)
   {
    LPTSTR pMapArray = (LPTSTR)MapViewOfFile(hMapMem,FILE_MAP_ALL_ACCESS, 0,0,0);
    if(pMapArray)
    { 
     int nGetDiff = (int)pMapArray[3];
    }
   }
  }
  
 }
 
 
 return TRUE;
}

 

 

4.关闭线程:

SetEvent(m_hNVMExit);
 Sleep(5);
 

 

发送端:

1.定义句柄:

 HANDLE m_hSdkdllEvent;
 HANDLE m_hSdkdllMapMem;
 BOOL InitMemRegion();

 

2.初始化:

m_hSdkdllMapMem = NULL;
 if(!InitMemRegion())
 {
  TRACE("Failed to create share memory region !/n");
  return;
 }
 //
 m_hSdkdllEvent = ::CreateEvent(NULL,FALSE,FALSE,_T("TEST"));
 //

 

3.相关函数和操作:

BOOL MediaPlayback::InitMemRegion()
{
 m_hSdkdllMapMem = ::CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE,0,1024,_T("CS_DATA"));
 if(m_hSdkdllMapMem == NULL)
  return FALSE;

 return TRUE;
}

 

////////////////////(在需要的位置添加)

 if(m_hSdkdllMapMem)
  {
   LPTSTR pMapArray = (LPTSTR)MapViewOfFile(m_hSdkdllMapMem,FILE_MAP_ALL_ACCESS, 0,0,0);
   if(Array[0] != '/0 ')
   {
    memcpy(pMapArray,Array,6);

    FlushViewOfFile(pMapArray,n);
    ::SetEvent(m_hSdkdllEvent);
   }
  }

/////////////////////////
     
    

 

4.关闭释放资源:

if(m_hSdkdllMapMem)
 {
  ::UnmapViewOfFile(m_hSdkdllMapMem);
  m_hSdkdllMapMem = NULL;
 }
 
 if(m_hSdkdllEvent)
 {
  ::CloseHandle(m_hSdkdllEvent);
 }
 

你可能感兴趣的:(object,File,null,Access,winapi)