1,知道接收消息进程的句柄。
2,接收消息进程重载了WM_COPYDATA消息映射
发送端
void CSendDlg::OnBnClickedSend()
{
CWnd *pWnd = CWnd::FindWindow(NULL,"接收窗口的标题");
CString sCopyData = "传递的字符串";
COPYDATASTRUCT cpd;
cpd.dwData = 0;
cpd.cbData = sCopyData.GetLength() + 1;//多加一个长度,防止乱码
cpd.lpData = (void*)sCopyData.GetBuffer(cpd.cbData);
pWnd->SendMessage(WM_COPYDATA,NULL,(LPARAM)&cpd);
}
{
CWnd *pWnd = CWnd::FindWindow(NULL,"接收窗口的标题");
CString sCopyData = "传递的字符串";
COPYDATASTRUCT cpd;
cpd.dwData = 0;
cpd.cbData = sCopyData.GetLength() + 1;//多加一个长度,防止乱码
cpd.lpData = (void*)sCopyData.GetBuffer(cpd.cbData);
pWnd->SendMessage(WM_COPYDATA,NULL,(LPARAM)&cpd);
}
// 声明
afx_msg BOOL OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct);
// 实现
ON_WM_COPYDATA()
BOOL CReceiveDlg::OnCopyData( CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct )
{
AfxMessageBox((LPCSTR)(pCopyDataStruct->lpData));
return CWnd::OnCopyData(pWnd, pCopyDataStruct);
}
afx_msg BOOL OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct);
// 实现
ON_WM_COPYDATA()
BOOL CReceiveDlg::OnCopyData( CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct )
{
AfxMessageBox((LPCSTR)(pCopyDataStruct->lpData));
return CWnd::OnCopyData(pWnd, pCopyDataStruct);
}
url:http://greatverve.cnblogs.com/archive/2012/12/17/WM_COPYDATA.html