本文主要是介绍MFC编程-通过WM_COPYDATA实现进程间通信,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
由于MSCOMM控件的注册问题,为了加快开发速度,就用上了WM_COPYDATA,很快就实现了进程间通信。
具体如下:
先写接收端源码,在三个地方做修改:
1.在头文件做函数声明:
afx_msg BOOL OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct);
2.在初始化函数里面做绑定
BEGIN_MESSAGE_MAP(CcommtestDlg, CDialogEx)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_WM_COPYDATA()
END_MESSAGE_MAP()
3.在文件中定义接收函数
BOOL CCopyDataReceiverDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{if (pCopyDataStruct->cbData > 0){char recvData[256] = {0} ;strncpy(recvData, (char *)pCopyDataStruct->lpData, pCopyDataStruct->cbData);SetDlgItemText(IDC_EDIT_RECEIVE, (char *)recvData);Feedback(pWnd);}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(); }
这样就实现了进程间的数据通信了。
这篇关于MFC编程-通过WM_COPYDATA实现进程间通信的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!