栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

[VC++]MFC使用WM

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

[VC++]MFC使用WM

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,%ldrn"),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);
}
四、效果与代码下载

源码下载地址:https://download.csdn.net/download/airen3339/35325668

示例源码下载

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/384994.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号