Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    3
    10
    290
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      mirro last edited by mirro

      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()
      {
      
      }
      
      
      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

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

        does it still crash ?

        M 1 Reply Last reply Reply Quote 1
        • M
          mirro @mrjj last edited by

          @mrjj

          Cannot the following code be used in another process?

          QWindow *container = QWindow::fromWinId((WId)hwnd);
          
          mrjj 1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion @mirro last edited by

            @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 ?

            M 1 Reply Last reply Reply Quote 0
            • M
              mirro @mrjj last edited by

              @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);
              
              B 1 Reply Last reply Reply Quote 0
              • B
                Bonnie @mirro last edited by

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

                M 1 Reply Last reply Reply Quote 0
                • M
                  mirro @Bonnie last edited by

                  @Bonnie How to convert Hwnd to Wid?

                  B 1 Reply Last reply Reply Quote 0
                  • B
                    Bonnie @mirro last edited by Bonnie

                    @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.

                    M 1 Reply Last reply Reply Quote 0
                    • M
                      mirro @Bonnie last edited by mirro

                      @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();
                      }
                      
                      B 1 Reply Last reply Reply Quote 0
                      • B
                        Bonnie @mirro last edited by

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

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post