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. Trying to use QT from DLL in simple console project but it doesn't work
Forum Update on Monday, May 27th 2025

Trying to use QT from DLL in simple console project but it doesn't work

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 4 Posters 742 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.
  • J Offline
    J Offline
    Jastot
    wrote on last edited by
    #1

    Well. I have find topic with a problem like my,but not the same...

    Trying to call the dynamic QWidget loader from simple console project - error.
    Which error? Heap is dead :D
    I have a look in disassembler and see that the pointer to QApplication is zero. (Think it blocked in anther tread...but dont enough experience to fix it)..

    Try to call my code from console project (copy+paste). all is ok. Back to dll - error.

    Can we use QT from DLL without create another treads or something like that???

    Code for example: (nevermind what is going on in classes which are in dll)

    QApplication* qapp = new QApplication(argc, argv);
    QtWidgetsClassTest* test = new QtWidgetsClassTest(); - QDialog
    FormLoader* formLoader = new FormLoader(qapp); - new uiLoader
    formLoader->uiLoader = new QUiLoader();
    formLoader->uiLoader->addPluginPath(qapp->applicationDirPath());
    

    So.

    addPluginPath(qapp->applicationDirPath());
    

    shows error...that heap is broken...

    1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      Hi, I've had success with starting Qt from a .dll belonging a console application. but I used QCoreApplication and also created it the usual way on the stack (i..e no new keyword).

      J 1 Reply Last reply
      0
      • hskoglundH hskoglund

        Hi, I've had success with starting Qt from a .dll belonging a console application. but I used QCoreApplication and also created it the usual way on the stack (i..e no new keyword).

        J Offline
        J Offline
        Jastot
        wrote on last edited by
        #3

        @hskoglund , Hi. Thank you for help.
        Try to do it like you write. Now compiler says true - that it can't use QApplication or QCoreApplication.

        Will try to read about treads and use something like that....cuz QApplication is blocked...
        But it is really interesting. i am create an QApplication on dll...and after that it writes that QAapp is blocked... But when i create all logic (not only QApp) in console project, which is calling dll - all is work.
        Best regards,
        Jastot.

        For example put some code here. If you have some ideas - i will be happy.

        part 1: app init in console and call func in dll

        #include <QtWidgets>
        #include <QApplication>
        #include <QtUIEditorDLL.h>
        int main(int argc, char* argv[])
        {
        	//QApplication
        	QApplication qapp(argc, argv);
        	QtUIEditorDLL* test = new QtUIEditorDLL(); - error Access violation reading location 
                return 0;
        }
        //////////
        QtUIEditorDLL::QtUIEditorDLL()
        {
        	FormLoader* formLoader = new FormLoader();
        	formLoader->uiLoader = new QUiLoader();
        	formLoader->uiLoader->addPluginPath(QApplication::applicationDirPath()); - error Access violation reading location 
        }
        

        part 2: call in dll

        int main(int argc, char* argv[])
        {
        	QtUIEditorDLL* test = new QtUIEditorDLL(); - error Access violation reading location 
                return 0;
        }
        //////////
        QtUIEditorDLL::QtUIEditorDLL()
        {
        int argc = 1;
        	char* argv[2] = { strdup("dummy"), {} };
        	QApplication qapp(argc, argv);
        	FormLoader* formLoader = new FormLoader();
        	formLoader->uiLoader = new QUiLoader();
        	formLoader->uiLoader->addPluginPath(QApplication::applicationDirPath()); - error Access violation reading location 
        }
        

        part 3: all in console project

        int main(int argc, char* argv[])
        {
        	QApplication qapp(argc, argv);
        	FormLoader* formLoader = new FormLoader();
        	formLoader->uiLoader = new QUiLoader();
        	formLoader->uiLoader->addPluginPath(QApplication::applicationDirPath()); - all is ok. it works.
                return 0;
        }
        
        jsulmJ 1 Reply Last reply
        0
        • J Jastot

          @hskoglund , Hi. Thank you for help.
          Try to do it like you write. Now compiler says true - that it can't use QApplication or QCoreApplication.

          Will try to read about treads and use something like that....cuz QApplication is blocked...
          But it is really interesting. i am create an QApplication on dll...and after that it writes that QAapp is blocked... But when i create all logic (not only QApp) in console project, which is calling dll - all is work.
          Best regards,
          Jastot.

          For example put some code here. If you have some ideas - i will be happy.

          part 1: app init in console and call func in dll

          #include <QtWidgets>
          #include <QApplication>
          #include <QtUIEditorDLL.h>
          int main(int argc, char* argv[])
          {
          	//QApplication
          	QApplication qapp(argc, argv);
          	QtUIEditorDLL* test = new QtUIEditorDLL(); - error Access violation reading location 
                  return 0;
          }
          //////////
          QtUIEditorDLL::QtUIEditorDLL()
          {
          	FormLoader* formLoader = new FormLoader();
          	formLoader->uiLoader = new QUiLoader();
          	formLoader->uiLoader->addPluginPath(QApplication::applicationDirPath()); - error Access violation reading location 
          }
          

          part 2: call in dll

          int main(int argc, char* argv[])
          {
          	QtUIEditorDLL* test = new QtUIEditorDLL(); - error Access violation reading location 
                  return 0;
          }
          //////////
          QtUIEditorDLL::QtUIEditorDLL()
          {
          int argc = 1;
          	char* argv[2] = { strdup("dummy"), {} };
          	QApplication qapp(argc, argv);
          	FormLoader* formLoader = new FormLoader();
          	formLoader->uiLoader = new QUiLoader();
          	formLoader->uiLoader->addPluginPath(QApplication::applicationDirPath()); - error Access violation reading location 
          }
          

          part 3: all in console project

          int main(int argc, char* argv[])
          {
          	QApplication qapp(argc, argv);
          	FormLoader* formLoader = new FormLoader();
          	formLoader->uiLoader = new QUiLoader();
          	formLoader->uiLoader->addPluginPath(QApplication::applicationDirPath()); - all is ok. it works.
                  return 0;
          }
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Jastot What is FormLoader? Please post the whole error message.
          Also, did you do debugging and check the stack trace after the crash?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Jastot What is FormLoader? Please post the whole error message.
            Also, did you do debugging and check the stack trace after the crash?

            J Offline
            J Offline
            Jastot
            wrote on last edited by Jastot
            #5

            @jsulm, hello.
            FormLoader - class with pointer on QUiLoader and some another functions...nothing more and nothing interesting.

            Full error looks like:
            Exception thrown at 0x00007FFCD0C91AFC (ntdll.dll) in .exe: 0xC0000005: Access violation reading location 0x0000000000000024.

            I read disassembled code and see that it crashes when trying to call QApplication...

            jsulmJ 1 Reply Last reply
            0
            • J Jastot

              @jsulm, hello.
              FormLoader - class with pointer on QUiLoader and some another functions...nothing more and nothing interesting.

              Full error looks like:
              Exception thrown at 0x00007FFCD0C91AFC (ntdll.dll) in .exe: 0xC0000005: Access violation reading location 0x0000000000000024.

              I read disassembled code and see that it crashes when trying to call QApplication...

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Jastot I think your problem is that you're allocating qapp as a local variable inside QtUIEditorDLL. That means it only exists inside that method, as soon as it terminates qapp is deleted. If you want to use Qt signals/slots you need to start Qt event loop calling qapp.exec().

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              J 1 Reply Last reply
              2
              • jsulmJ jsulm

                @Jastot I think your problem is that you're allocating qapp as a local variable inside QtUIEditorDLL. That means it only exists inside that method, as soon as it terminates qapp is deleted. If you want to use Qt signals/slots you need to start Qt event loop calling qapp.exec().

                J Offline
                J Offline
                Jastot
                wrote on last edited by
                #7

                @jsulm
                Well. Sadly it didn't help.
                So i tried to some experiments...
                I found that QApplication works in dll without exec().

                now QtUIEditorDLL class is QApplication.

                class QTUIEDITORDLL_EXPORT QtUIEditorDLL: QApplication
                

                In console app i have made something like that: (believe that pointer can help...)

                std::shared_ptr<QtUIEditorDLL> ptrQtUIEditorDLL(new QtUIEditorDLL(argc, argv));
                	QtUIEditorDLL* test = ptrQtUIEditorDLL.get();
                	test->Init();
                

                and in dll made something like that:

                QtUIEditorDLL::QtUIEditorDLL(int& argc, char** argv)
                	:QApplication(argc,argv) {}
                
                void QtUIEditorDLL::Init()
                {
                	QtWidgetsClassTest* test = new QtWidgetsClassTest(); // QDialog initialization worked......So QApplication works
                	QUiLoader quiLoader;
                	QString qstr = QCoreApplication::applicationDirPath(); // All is ok. QApplication works. in qstr i can see true way to application.
                	quiLoader.addPluginPath(qstr); // same error... Exception thrown at 0x00007FFCD0C91AFC (ntdll.dll) in .exe: 0xC0000005: Access violation reading location 0x0000000000000024.
                

                So...its problem of QUiLoader...or am i not right?
                Sorry for stupid questions...

                jsulmJ 1 Reply Last reply
                0
                • J Jastot

                  @jsulm
                  Well. Sadly it didn't help.
                  So i tried to some experiments...
                  I found that QApplication works in dll without exec().

                  now QtUIEditorDLL class is QApplication.

                  class QTUIEDITORDLL_EXPORT QtUIEditorDLL: QApplication
                  

                  In console app i have made something like that: (believe that pointer can help...)

                  std::shared_ptr<QtUIEditorDLL> ptrQtUIEditorDLL(new QtUIEditorDLL(argc, argv));
                  	QtUIEditorDLL* test = ptrQtUIEditorDLL.get();
                  	test->Init();
                  

                  and in dll made something like that:

                  QtUIEditorDLL::QtUIEditorDLL(int& argc, char** argv)
                  	:QApplication(argc,argv) {}
                  
                  void QtUIEditorDLL::Init()
                  {
                  	QtWidgetsClassTest* test = new QtWidgetsClassTest(); // QDialog initialization worked......So QApplication works
                  	QUiLoader quiLoader;
                  	QString qstr = QCoreApplication::applicationDirPath(); // All is ok. QApplication works. in qstr i can see true way to application.
                  	quiLoader.addPluginPath(qstr); // same error... Exception thrown at 0x00007FFCD0C91AFC (ntdll.dll) in .exe: 0xC0000005: Access violation reading location 0x0000000000000024.
                  

                  So...its problem of QUiLoader...or am i not right?
                  Sorry for stupid questions...

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Jastot said in Trying to use QT from DLL in simple console project but it doesn't work:

                  So...its problem of QUiLoader...or am i not right?

                  Since you did not post the stack trace after the crash I don't know.
                  Subclassing QApplication is also not needed - adding qapp as class member should be enough.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  J 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @Jastot said in Trying to use QT from DLL in simple console project but it doesn't work:

                    So...its problem of QUiLoader...or am i not right?

                    Since you did not post the stack trace after the crash I don't know.
                    Subclassing QApplication is also not needed - adding qapp as class member should be enough.

                    J Offline
                    J Offline
                    Jastot
                    wrote on last edited by
                    #9

                    @jsulm

                    30e285ee-07af-4633-9b95-53c1e18c45ce-image.png

                    jsulmJ 1 Reply Last reply
                    0
                    • J Jastot

                      @jsulm

                      30e285ee-07af-4633-9b95-53c1e18c45ce-image.png

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Jastot Is it still crashing if you allocate quiLoader on the heap (just for testing)?

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      J 2 Replies Last reply
                      0
                      • jsulmJ jsulm

                        @Jastot Is it still crashing if you allocate quiLoader on the heap (just for testing)?

                        J Offline
                        J Offline
                        Jastot
                        wrote on last edited by
                        #11

                        @jsulm
                        Yes. Allocate it on the heap and the same error.

                        1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @Jastot Is it still crashing if you allocate quiLoader on the heap (just for testing)?

                          J Offline
                          J Offline
                          Jastot
                          wrote on last edited by Jastot
                          #12

                          Well, @jsulm , I am very grateful to you. Thank you.
                          I have found how to fix it and don't destroy logic.

                          JonBJ 1 Reply Last reply
                          0
                          • J Jastot has marked this topic as solved on
                          • J Jastot

                            Well, @jsulm , I am very grateful to you. Thank you.
                            I have found how to fix it and don't destroy logic.

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #13

                            @Jastot said in Trying to use QT from DLL in simple console project but it doesn't work:

                            I have found how to fix it

                            Wouldn't you like to share that with us, and maybe benefit future readers? :)

                            J 1 Reply Last reply
                            1
                            • JonBJ JonB

                              @Jastot said in Trying to use QT from DLL in simple console project but it doesn't work:

                              I have found how to fix it

                              Wouldn't you like to share that with us, and maybe benefit future readers? :)

                              J Offline
                              J Offline
                              Jastot
                              wrote on last edited by
                              #14

                              @JonB
                              OPS. forget about it:D

                              The main problem wasn't in QApplication as I thought initially.
                              QUiLoader didn't want to work and showed an error that there was no access to the address.

                              So i created QApplication and QUiLoader in console project and then transfer pointer to library.
                              I still have a question why QCoreApplication::applicationDirPath(); is ok but quiLoader->addPluginPath(qstr); which calls QApplication, is not ok...

                              Simple Console Project:

                              int main(int argc, char* argv[])
                              {
                              	std::shared_ptr<QtUIEditorDLL> ptrQtUIEditorDLL(new QtUIEditorDLL(argc, argv)); // you can use it without any ptr.
                              	QtUIEditorDLL* test = ptrQtUIEditorDLL.get();
                              	QUiLoader* quiLoader = new QUiLoader(); 
                              // as i can see it must be created in the same tread near QApp. Jsulm said that it not important...but it doesn't work if create it in DLL.
                              	test->Init(quiLoader);
                              }
                              

                              And the library:

                              QtUIEditorDLL::QtUIEditorDLL(int& argc, char** argv)
                              	:QApplication(argc,argv){}
                              void QtUIEditorDLL::Init(QUiLoader* quiLoader)
                              {
                              	QString qstr = QCoreApplication::applicationDirPath();
                              	quiLoader->addPluginPath(qstr);
                                      //And it works. 
                              }
                              
                              1 Reply Last reply
                              1

                              • Login

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