[VC++]MFC使用WM_COPYDATA消息进行进程间的通讯

8 篇文章 0 订阅

MFC使用WM_COPYDATA消息进行进程间的通讯

本代码实现了WM_COPYDATA数据的传递及参数 (WPARAM) 与 (LPARAM)的接收,尤其是如何接收WPARAM消息,本示例给出了接收代码。

一、介绍
Windows上MFC应用程序可使用WM_COPYDATA可以完成两个进程之间的通讯。
当一个应用向另一个应用传送数据时,发送方需调用SendMessage函数,参数是目的窗口的句柄、传递数据的起始地址、WM_COPYDATA消息。接收方只需响应WM_COPY DATA消息,双方就实现了数据共享。
它在底层实际上是通过文件映射来实现的,缺点是灵活性不高,并且它只能用于Windows平台的单机环境下。
其中的copyData是要发送的数据,类型为COPYDATASTRUCT结构体:

typedef struct tagCOPYDATASTRUCT



DWORD dwData;  

DWORD cbData;

PVOID lpData;

} COPYDATASTRUCT;



dwData : Specifies up to 32 bits of data to be passed to the receiving application.

cbData : Specifies the size, in bytes, of the data pointed to by the lpData member.

lpData : Long pointer to data to be passed to the receiving application. This member can be NULL.

该消息只能由SendMessage()发送,而不能使用PostMessage()。因为系统必须管理用以传递数据的缓冲区的生命期,如果使用了PostMessage(),数据缓冲区会在接收方(线程)有机会处理该数据之前,就被系统清除和回收。

如果传入的接收窗口句柄无效或者当接收方进程意外终止时,SendMessage()会立即返回,发送方不会陷入一个无穷等待的状态中。

此外还需注意:

The data being passed must not contain pointers or other references to objects not accessible to the application receiving the data.(所发送的数据不能包含数据接收方无法访问的指针或对象引用)

While this message is being sent, the referenced data must not be changed by another thread of the sending process.(消息发送后,要保证lpData所引用数据不能被其它线程修改(直到SendMessage函数返回))
二、发送端界面与代码示例


void CClientDlg::OnButton1() 
{
        // TODO: Add your control notification handler code here
        CString strMsg;
    GetDlgItem(IDC_EDIT_SendData)->GetWindowText(strMsg);

    CWnd* serverWnd = CWnd::FindWindow(NULL, "server");
    if (serverWnd)
    {
                WORD wIdent=1002;
                int nParam = 0;
                nParam = MAKELONG(0, wIdent);
                
                WORD wIdent2 = HIWORD(nParam);// 高位字节
                WORD wRecog2 = LOWORD(nParam);//低位字节

                CString str;
                str.Format(_T("%ld,%ld,%ld,%ld"),nParam,wIdent,wIdent2,wRecog2);
                //        AfxMessageBox(str);
                GetDlgItem(IDC_EDIT_NPARAM)->SetWindowText(str);
                

        COPYDATASTRUCT cpd;
        cpd.dwData = 0;//用户定义的数据类型,可以用来作为发送标志
        //        cpd.cbData = strMsg.GetLength() * sizeof(TCHAR);//数据大小,长度一定要满足,否则数据传输不全
                cpd.cbData = strMsg.GetLength()+1;
        cpd.lpData = (void*)strMsg.GetBuffer(cpd.cbData); //数据指针
        LRESULT copyDataResult = ::SendMessage(serverWnd->m_hWnd, WM_COPYDATA, (WPARAM)nParam, (LPARAM)&cpd);//发送消息  serverWnd->GetSafeHwnd()
        strMsg.ReleaseBuffer();
    }
}

三、接收端界面与代码示例

在对话框界面,右键对话框->属性,选择消息选项卡,添加WM_COPYDATA消息,如下图

消息函数代码如下:

BOOL CServerDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct) 
{
        // TODO: Add your message handler code here and/or call default
        WORD wIdent = 0, wRecog = 0;
        int pInt = (int)pWnd->m_hWnd;//传递数据;
        //取得相应数据
        wIdent = HIWORD(pInt);// 高字节数据
        wRecog = LOWORD(pInt);// 低字节数据

        CString str;
        str.Format(_T("%d,%ld,%ld\r\n"),pInt,wIdent,wRecog);
        GetDlgItem(IDC_EDIT_NPARAM)->SetWindowText(str);


        LPCTSTR lstrMsg = (LPCTSTR)(pCopyDataStruct->lpData);
    CString strMsg(lstrMsg);
    GetDlgItem(IDC_EDIT_ReceiveData)->SetWindowText(strMsg);

        return CDialog::OnCopyData(pWnd, pCopyDataStruct);
}
四、效果与代码下载
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

依星net188.com

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值