zoukankan      html  css  js  c++  java
  • MFC sendmessage实现进程间通信

    用sendmessage实现进程间通信。


    1.WM_COPYDATA实现进程间通信

    实现方式是发送WM_COPYDATA消息。

    发送程序:

    LRESULT copyDataResult;  //copyDataResult has value returned by other app 
    CWnd *pOtherWnd = CWnd::FindWindow(NULL, "卡口图片管理");
    
    CString strDataToSend = "0DAE12A3D8C9425DAAE25B3ECD16115A" ;
    if (pOtherWnd)
    {
        COPYDATASTRUCT cpd;
        cpd.dwData = 0;
        cpd.cbData = strDataToSend.GetLength()+sizeof(wchar_t);             //data length
        cpd.lpData = (void*)strDataToSend.GetBuffer(cpd.cbData); //data buffer
        copyDataResult = pOtherWnd->SendMessage(WM_COPYDATA,(WPARAM)AfxGetApp()->m_pMainWnd->GetSafeHwnd(),(LPARAM)&cpd);
        strDataToSend.ReleaseBuffer();
    }
    else 
    {
        AfxMessageBox("Unable to find other app.");
    }

    这里字符串长度为strDataToSend.GetLength()+sizeof(wchar_t),其中sizeof(wchar_t)指 的长度。

    接收程序:

         接收程序先给窗口(我这里的窗口名叫“卡口图片管理”)添加WM_COPYDATA消息函数,然后在函数中添加成如下:

    BOOL CCarRecogDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
    {
        // TODO: 在此添加消息处理程序代码和/或调用默认值
    
        CString strRecievedText = (LPCSTR) (pCopyDataStruct->lpData);
        string str(strRecievedText.GetBuffer(pCopyDataStruct->cbData));
        //string str = strRecievedText.GetBuffer() ;
        printf("%s 
    ", str.c_str()) ;
    
        if (str == "0DAE12A3D8C9425DAAE25B3ECD16115A")
        {
            printf("正确接收 
    ") ;
        }
    
        return CDialogEx::OnCopyData(pWnd, pCopyDataStruct);
    }


    运行结果:

    image


    2.win32程序和x64程序之间传递结构体

    win32程序和x64程序的指针长度是不一样的。经大神指点,结构体中的成员变量只能是基本数据类型,不能是像string这样的类,大体原因是类中有函数,函数也是一个地址(说法貌似不严谨,大概是这个意思)。

    下面在之前的基础上实现传递结构体。

    结构体定义:

    struct NOTIFY_INFO_t
    {
        char  GUID[1024];
        char  task_id[1024];
        int  index ;
    };

    收发程序中的结构体定义要一致(结构体名可以不一致,但内部需要一致)。

    主要的注意点是结构体成员变量的定义,具体的收发其实差不多。

    发送:

    void CMFCApplication1Dlg::OnBnClickedButtonSend()
    {
        // TODO: 在此添加控件通知处理程序代码
    
        LRESULT copyDataResult;  //copyDataResult has value returned by other app 
        CWnd *pOtherWnd = CWnd::FindWindow(NULL, "卡口图片管理");
    
        CString strDataToSend = "0DAE12A3D8C9425DAAE25B3ECD16115A" ;
         if (pOtherWnd)
        {
            NOTIFY_INFO_t *pNotify = new NOTIFY_INFO_t();
            memcpy(pNotify->GUID,"0DAE12A3D8C9425DAAE25B3ECD16115A",sizeof("0DAE12A3D8C9425DAAE25B3ECD16115A")) ;
    
            COPYDATASTRUCT cpd;
            cpd.dwData = 0;
            cpd.cbData = sizeof(NOTIFY_INFO_t);             //data length
            cpd.lpData = (void*)pNotify;                //data buffer
            copyDataResult = pOtherWnd->SendMessage(WM_COPYDATA,(WPARAM)AfxGetApp()->m_pMainWnd->GetSafeHwnd(),(LPARAM)&cpd);
            strDataToSend.ReleaseBuffer();
        }
        else 
        {
            AfxMessageBox("Unable to find other app.");
        }
    }

    接收:

    BOOL CCarRecogDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
    {
        // TODO: 在此添加消息处理程序代码和/或调用默认值
    
        NOTIFY_INFO_t *pNotify ;
        pNotify = (NOTIFY_INFO_t *)pCopyDataStruct->lpData ;
    
        printf("%s 
    ",pNotify->GUID) ;
    
        return CDialogEx::OnCopyData(pWnd, pCopyDataStruct);
    }

    运行结果:

    image

  • 相关阅读:
    四、元数据ParameterMetaData
    三、PreparedStatement对象(重点)
    二、properties文件存储信息
    一、JDBC3.0标准中常用接口与类
    十一、MySQL 数据库存储引擎介绍
    十、执行计划
    注册表中一些项目拒绝访问,拒绝更改权限,拒绝删除
    x64dbg 的条件断点及断点日志
    手机APP无法抓包HTTPS解决方案
    原版黑苹果安装(个人备忘)
  • 原文地址:https://www.cnblogs.com/betterwgo/p/8441452.html
Copyright © 2011-2022 走看看