Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Why does QT DLL process calls this class from .exe process give an access violation?



  • the following link Error screenshot
    link Error screenshot

    //dll code

    #include <QtWidgets/QWidget>
    #include "qtdll_global.h"
    
    class QTDLL_EXPORT widgetdll : public QWidget
    {
    	Q_OBJECT
    
    public:
    	widgetdll(QWidget *parent = 0,HWND hwnd = 0);
    	~widgetdll();
    };
    #include "widgetdll.h"
    #include <QWindow>
    #include <QMessageBox>
    widgetdll::widgetdll(QWidget *parent,HWND hwnd)
    	: QWidget(parent)
    {
            setFixedSize(600, 400);
    	//
    	QWindow *container = QWindow::fromWinId((WId)hwnd);
    
    	container->show();
    }
    
    widgetdll::~widgetdll()
    {
    
    }
    

    .exe code

    #include <QtWidgets/QMainWindow>
    #include "ui_callDll.h"
    
    class callDll : public QMainWindow
    {
    	Q_OBJECT
    
    public:
    	callDll(QWidget *parent = 0);
    	~callDll();
    
    private:
    	Ui::callDllClass ui;
    };
    
    #include "callDll.h"
    #include "widgetdll.h"
    #include <QtWidgets/QLayout>
    #include <QWindow>
    #include <QPushButton>
    callDll::callDll(QWidget *parent)
    	: QMainWindow(parent)
    {
    	ui.setupUi(this);
    	QWidget  *pUserWidget = new QWidget;
    	pUserWidget->setObjectName(QString::fromUtf8("Sub Window"));
    	pUserWidget->setFixedSize(600, 400);
    	pUserWidget->setWindowTitle("Sub Window");
    
    	WId hwndParentWidget = pUserWidget->effectiveWinId();
    	widgetdll * p = new widgetdll(0,(HWND)hwndParentWidget);
    	p->show();
    
    	setWindowTitle("Parent Window");
    	setObjectName("Parent Window");
    }
    
    callDll::~callDll()
    {
    
    }
    
    

  • Lifetime Qt Champion

    Hi
    If you do
    QWindow *container = QWindow::fromWinId((WId)hwnd);
    if ( container )
    container->show();

    does it still crash ?



  • @mrjj

    Cannot the following code be used in another process?

    QWindow *container = QWindow::fromWinId((WId)hwnd);
    

  • Lifetime Qt Champion

    @mirro said in Why does QT DLL process calls this class from .exe process give an access violation?:

    fromWinId

    Docs sounds like it can.
    https://doc.qt.io/qt-5/qwindow.html#fromWinId

    so if you check for NULL you no longer crash ?



  • @mrjj

    If the null pointer is checked, the program does not crash.

    Can't the following code be used in another process?

    QWindow *container = QWindow::fromWinId((WId)hwnd);
    


  • @mirro
    Have you checked that if hwnd / hwndParentWidget is a valid value?



  • @Bonnie How to convert Hwnd to Wid?



  • @mirro
    I don't mean that.
    I've tested your code with

        QWidget  *pUserWidget = new QWidget;
        pUserWidget->setObjectName(QString::fromUtf8("Sub Window"));
        pUserWidget->setFixedSize(600, 400);
        pUserWidget->setWindowTitle("Sub Window");
    
        WId hwndParentWidget = pUserWidget->effectiveWinId();
        qDebug() << hwndParentWidget;
    

    And the value of hwndParentWidget is 0.
    So I ask you if you've checked that.
    If the hwnd is 0, then it is reasonable that the result of QWindow::fromWinId is nullptr.



  • @Bonnie
    thank you.
    is (HWND)hwndParentWidget null value?
    How to convert WId to HWND?

    callDll::callDll(QWidget *parent)
    	: QMainWindow(parent)
    {
    	ui.setupUi(this);
    	//
    	QWidget  *pUserWidget = new QWidget;
    	pUserWidget->setObjectName(QString::fromUtf8("another progress widget"));
    	pUserWidget->setFixedSize(600, 400);
    	pUserWidget->setWindowTitle("another progress widget");
    	WId hwndParentWidget = pUserWidget->effectiveWinId();
    	widgetdll * p = new widgetdll(0,(HWND)hwndParentWidget);
    	p->show();
    }
    
    widgetdll::widgetdll(QWidget *parent,HWND hwnd)
    	: QWidget(parent)
    {
    	//
    	QWindow *container = QWindow::fromWinId((WId)hwnd);
    	if (container)
    		container->show();
    }
    


  • @mirro
    Your conversion has no problem.
    But I think you should use winId() instead of effectiveWinId()


Log in to reply