weixin_45014393
weixin_45014393
2019-05-08 16:17

MFC的exe调用qt的dll界面。现在是通过WM_COPYDATA发消息给qt,但是qt没有收到消息

20
  • c++

请各位大佬帮忙看看是不是代码有问题,为什么qt的界面收不到xiao'xi

这是qt收消息的代码

#include "meminnetdlg.h"
#include
#include
#include "qstring.h"
#include "QListWidget"
#include
#include
#include
MemInNetDlg::MemInNetDlg(QWidget *parent)
: QDialog(parent),ui(new Ui::MemInNetDlg)
{
ui->setupUi(this);
}

MemInNetDlg::~MemInNetDlg()
{

}

bool MemInNetDlg::nativeEvent(const QByteArray &eventType,void message,long *result)
{
MSG*pMsg =static_cast<MSG
>(message);
if(pMsg&&pMsg->message==WM_COPYDATA)
{
qDebug()<<"收到WM_COPYDATA消息";
COPYDATASTRUCT TmpData= reinterpret_cast<COPYDATASTRUCT>(pMsg->lParam);
QString text1=QString("发来的消息为:%1").arg((char*)TmpData->lpData);
ui->listWidget->addItem(text1);
if(TmpData)
{
QByteArray tmpstr = QByteArray(reinterpret_cast(TmpData->lpData),TmpData->cbData);
QMessageBox ::information(NULL,"recvMsg",QString("int:%1 string:%2").arg(tmpstr.data()));
}
return true;
}
}

void CQTDEMODlg::OnBnClickedButton1()
{
    // TODO: 在此添加控件通知处理程序代码
    DLLFUN  fun;
    HINSTANCE hdll;
    hdll=LoadLibrary("qtdialog.dll");
    if(hdll==NULL)
    {
        FreeLibrary(hdll);
    }
    fun=(DLLFUN)GetProcAddress(hdll,"showDialog");
    if(fun==NULL)
    {
        FreeLibrary(hdll);
    }
    bool a=fun(this->m_hWnd);
        FreeLibrary(hdll);
}

这一部分是mfc用来给qt发消息的代码
void CQTDEMODlg::OnBnClickedButton2()
{
    // TODO: 在此添加控件通知处理程序代码


    //SetDlgItemInt(IDC_BUTTON1,a);
    LRESULT copyDataResult;
    LPCSTR strWindowTitle= "meminnetdlg";
    CWnd *accWnd =CWnd::FindWindow(NULL,strWindowTitle);
    if(accWnd)
    {
        CString strDataToSend = "12345";
        COPYDATASTRUCT copydata;
        copydata.dwData=0;
        copydata.cbData=strDataToSend.GetLength();
        copydata.lpData=(void*)strDataToSend.GetBuffer(copydata.cbData);
        copyDataResult =accWnd->SendMessage(WM_COPYDATA,(WPARAM)AfxGetApp()->m_pMainWnd->GetSafeHwnd(),(LPARAM)&copydata);
        strDataToSend.ReleaseBuffer();
    }
    else
    {
        AfxMessageBox("unable to find other window");
    }

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答