我正在尝试使用共享内存在两个进程之间进行通信,我想传输一个CObject对象。
我有
class CMyObject : public CObject
{
public:
CMyObject();
virtual ~CMyObject();
public:
int m_nId;
CString m_sData;
};
和这个对象,我打算通过IPC发送:
if(NULL == m_pMyObject)
{
m_pMyObject = new CMyObject;
m_pMyObject->m_nId = 24;
m_pMyObject->m_sData = sBuffer;
}
和
HANDLE hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 0, lpszMappingName);
LPVOID lpData = MapViewOfFile(hMapping, FILE_MAP_WRITE, 0, 0, sizeof(CMyObject));
if(NULL != lpData)
CopyMemory(lpData, m_pMyObject, sizeof(CMyObject));
似乎有效。
但是当我尝试在第二个过程中读取CMyObject数据时:
HANDLE hMapping = OpenFileMapping(FILE_MAP_READ, 0, pszMappingName);
CMyObject* lpData = (CMyObject*)MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, sizeof(CMyObject));
if(NULL != lpData)
TRACE(_T("%s"), lpData->m_sData);
有时候会崩溃,什么都没有得到......你能给我一点帮助吗? 谢谢。