Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why does QT DLL process calls this class from .exe process give an access violation?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 688 Views
  • 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 Offline
    M Offline
    mirro
    wrote on last edited by mirro
    #1

    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
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      does it still crash ?

      M 1 Reply Last reply
      1
      • mrjjM mrjj

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

        does it still crash ?

        M Offline
        M Offline
        mirro
        wrote on last edited by
        #3

        @mrjj

        Cannot the following code be used in another process?

        QWindow *container = QWindow::fromWinId((WId)hwnd);
        
        mrjjM 1 Reply Last reply
        0
        • M mirro

          @mrjj

          Cannot the following code be used in another process?

          QWindow *container = QWindow::fromWinId((WId)hwnd);
          
          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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
          0
          • mrjjM mrjj

            @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 Offline
            M Offline
            mirro
            wrote on last edited by
            #5

            @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
            0
            • M mirro

              @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 Offline
              B Offline
              Bonnie
              wrote on last edited by
              #6

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

              M 1 Reply Last reply
              0
              • B Bonnie

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

                M Offline
                M Offline
                mirro
                wrote on last edited by
                #7

                @Bonnie How to convert Hwnd to Wid?

                B 1 Reply Last reply
                0
                • M mirro

                  @Bonnie How to convert Hwnd to Wid?

                  B Offline
                  B Offline
                  Bonnie
                  wrote on last edited by Bonnie
                  #8

                  @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
                  0
                  • B 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 Offline
                    M Offline
                    mirro
                    wrote on last edited by mirro
                    #9

                    @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
                    0
                    • M 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 Offline
                      B Offline
                      Bonnie
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved