http://blog.csdn.net/ao929929fei/article/details/6316174
接收資料的一方
ON_WM_COPYDATA()
afx_msg BOOL OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct);
程式碼片段:
- 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);
- }
傳送資料的一方
程式碼片段:
- 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()函式,接收方在資料複製完成前不返回,這樣傳送方就不可能刪除和修改資料。