进程A和进程B之间共享如下一个struct:
struct m_MapView_Struct{
char m_c;
int m_iMapView;
char m_pszMapView[MAX_BUFFER_SIZE];
char m_pszMapView2[MAX_BUFFER_SIZE];
}* m_pSMapView;
进程A中:
m_hMapObject=CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,
PAGE_READWRITE, 0, 0x1000, _TEXT("shared_memory"));
if(!m_hMapObject){
AfxMessageBox("Unable to create shared memory file.");
return FALSE;
}
// Map view of file into our address space.
m_pSMapView=(m_MapView_Struct *)MapViewOfFile
(m_hMapObject,FILE_MAP_WRITE, 0, 0,0);
if(m_pSMapView==NULL){
AfxMessageBox("Unable to map shared memory file.");
return FALSE;
}
m_pSMapView->m_iMapView = 13628;
m_pSMapView->m_c = '*';
lstrcpy(m_pSMapView->m_pszMapView, "c++");
lstrcpy(m_pSMapView->m_pszMapView2, "stl wlt atl");
进程B中:
// Open memory mapped file.
m_hMapObject=OpenFileMapping(FILE_MAP_READ,FALSE,_TEXT("shared_memory"));
if(!m_hMapObject){
AfxMessageBox("Can't open shared memory file,please run 'Send' first.");
SendMessage(WM_CLOSE);
return FALSE;
}
// Get pointer to shared data.
m_pSMapView=(m_MapView_Struct *)MapViewOfFile
(m_hMapObject,FILE_MAP_READ, 0, 0,0);
if(m_pSMapView==NULL)
AfxMessageBox("Can't map view of shared memory file.");
m_strText.Format("%d,%s,%s,%c",m_pSMapView->m_iMapView, m_pSMapView->m_pszMapView
,m_pSMapView->m_pszMapView2 ,m_pSMapView->m_c);