WM_COPYDATA: CString strWindowTitle = _T("Window Name"); CString strDataToSend = _T("This is a message to send");
LRESULT copyDataResult; CWnd *pOtherWnd = CWnd::FindWindow(NULL, strWindowTitle); if (pOtherWnd) { COPYDATASTRUCT cpd; cpd.dwData = 0; cpd.cbData = strDataToSend.GetLength(); cpd.lpData = (void*)strDataToSend.GetBuffer(cpd.cbData); copyDataResult = pOtherWnd->SendMessage(WM_COPYDATA, (WPARAM)AfxGetApp()->m_pMainWnd->GetSafeHwnd(), (LPARAM)&cpd); strDataToSend.ReleaseBuffer(); // copyDataResult has value returned by other app
} else { AfxMessageBox("Unable to find other app."); } }The other app should handle the WM_COPYDATA message in the following manner BEGIN_MESSAGE_MAP(CMyWnd, CWnd) //{{AFX_MSG_MAP(CMyWnd) ... ON_WM_COPYDATA() ... //}}AFX_MSG_MAP END_MESSAGE_MAP()...BOOL CMyWnd::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct) { CString strRecievedText = (LPCSTR) (pCopyDataStruct->lpData);
CString strWindowTitle = _T("Window Name");
CString strDataToSend = _T("This is a message to send");
LRESULT copyDataResult;
CWnd *pOtherWnd = CWnd::FindWindow(NULL, strWindowTitle); if (pOtherWnd)
{
COPYDATASTRUCT cpd;
cpd.dwData = 0;
cpd.cbData = strDataToSend.GetLength();
cpd.lpData = (void*)strDataToSend.GetBuffer(cpd.cbData);
copyDataResult = pOtherWnd->SendMessage(WM_COPYDATA,
(WPARAM)AfxGetApp()->m_pMainWnd->GetSafeHwnd(),
(LPARAM)&cpd);
strDataToSend.ReleaseBuffer();
// copyDataResult has value returned by other app
}
else
{
AfxMessageBox("Unable to find other app.");
}
}The other app should handle the WM_COPYDATA message in the following manner BEGIN_MESSAGE_MAP(CMyWnd, CWnd)
//{{AFX_MSG_MAP(CMyWnd)
...
ON_WM_COPYDATA()
...
//}}AFX_MSG_MAP
END_MESSAGE_MAP()...BOOL CMyWnd::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{
CString strRecievedText = (LPCSTR) (pCopyDataStruct->lpData);
return CMyWnd::OnCopyData(pWnd, pCopyDataStruct);
}
WPARAM wParam = (WPARAM)strSource.GetBuffer(strSource.GetLength());
strSource.ReleaseBuffer();CString strFileName = "file name";
LPARAM lParam=(LPARAM)strFileName.GetBuffer(strFileName.GetLength());
strFileName.ReleaseBuffer();
SendMessage(WM_SHOWFRM_DELETEITEM, wParam, lParam);