进程间通信的WM_COPYDATA的使用
http://blog.csdn.net/ao929929fei/article/details/6316174
接收数据的一方
ON_WM_COPYDATA()
afx_msg BOOL OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct);
代码片段:
[cpp] view plain copy
- BOOL CClientDemoDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
- {
- if (pCopyDataStruct != NULL)
- {
- LPCTSTR pszText = (LPCTSTR)(pCopyDataStruct->lpData);
- DWORD dwLength = (DWORD)(pCopyDataStruct->cbData);
- TCHAR szRecvText[1024] = {0};
- memcpy(szRecvText, pszText, dwLength);
- MessageBox(szRecvText, _T(“Test”), MB_OK);
- }
- return CDialog::OnCopyData(pWnd, pCopyDataStruct);
- }
发送数据的一方
代码片段:
[cpp] view plain copy
- CString strWindowTitle = _T(“ClientDemo”);
- CString strDataToSend = _T(“Hello, Everyone fsdfsdfsdfdsfds,this is a test demo!”);
- HWND hOtherWnd = ::FindWindow(NULL, strWindowTitle.GetBuffer(0));
- if (hOtherWnd != NULL && ::IsWindow(hOtherWnd))
- {
- COPYDATASTRUCT cpd;
- cpd.dwData = 0;
- cpd.cbData = strDataToSend.GetLength() * sizeof(TCHAR);
- cpd.lpData = (void*)strDataToSend.GetBuffer(0);
- HRESULT hResult = ::SendMessage(hOtherWnd, WM_COPYDATA, (WPARAM)(AfxGetApp()->m_pMainWnd), (LPARAM)&cpd);
- strDataToSend.ReleaseBuffer();
- }
OK了,就这么简单
WM_COPYDATA消息主要目的是允许在进程间传递少量只读数据。SDK文档推荐用户使用SendMessage()函数,接收方在数据复制完成前不返回,这样发送方就不可能删除和修改数据。
还没有评论,来说两句吧...