Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. When calling the Shared Libraries (DLL) created by QT6, the main program will crash
Forum Updated to NodeBB v4.3 + New Features

When calling the Shared Libraries (DLL) created by QT6, the main program will crash

Scheduled Pinned Locked Moved Solved Qt 6
18 Posts 5 Posters 2.2k Views 1 Watching
  • 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.
  • pedisChenP Offline
    pedisChenP Offline
    pedisChen
    wrote on last edited by
    #1

    I created a DLL file with QT6, which uses the QTCPSocket network component. When I call the function that uses QT components in DLL with QLibrary, the main program will crash, such as connect_to_server() function.

    I want to know how to implement QT components such as QTCPSocket or UI components such as Widget in the DLL file, so that I can use the DLL file on other platforms (VC or the others)

    Thanks!

    1 Reply Last reply
    0
    • J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      You probably have neglected to provide the dll with the dll's it itself needs. You'll have to run win deploy qt on your dll just as you would with a normal application


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      pedisChenP 2 Replies Last reply
      0
      • J.HilkJ J.Hilk

        You probably have neglected to provide the dll with the dll's it itself needs. You'll have to run win deploy qt on your dll just as you would with a normal application

        pedisChenP Offline
        pedisChenP Offline
        pedisChen
        wrote on last edited by
        #3

        @J-Hilk Thank you for your relpy. I tried, like this
        C:\Qt\Tools\QtDesignStudio\qt6_design_studio_reduced_version\bin>windeployqt6.exe D:\documents\zxin\dll\build-myDLL-Desktop_Qt_6_5_3_MinGW_64_bit-Release\release\myDLL.dll
        Then I added the generated file to my project directory, but it's still the same.
        My dll file was built with QT6, and then I created another project with QT6 and tried to call it

        Pl45m4P 1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          You probably have neglected to provide the dll with the dll's it itself needs. You'll have to run win deploy qt on your dll just as you would with a normal application

          pedisChenP Offline
          pedisChenP Offline
          pedisChen
          wrote on last edited by
          #4

          @J-Hilk I think it is because the dll lacks of an QT event loop mechanism. so I added an init function in dll,and need to call the init function first , defined like this:
          QCoreApplication a(argc, argv);
          return a.exec();

          This worked, but it would always block the main program. Then I planned to use QThread to create a new process in the dll to run a.exec(), but it failed and the new process task could not run.I dont know why...

          1 Reply Last reply
          0
          • pedisChenP pedisChen

            @J-Hilk Thank you for your relpy. I tried, like this
            C:\Qt\Tools\QtDesignStudio\qt6_design_studio_reduced_version\bin>windeployqt6.exe D:\documents\zxin\dll\build-myDLL-Desktop_Qt_6_5_3_MinGW_64_bit-Release\release\myDLL.dll
            Then I added the generated file to my project directory, but it's still the same.
            My dll file was built with QT6, and then I created another project with QT6 and tried to call it

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #5

            @pedisChen said in When calling the Shared Libraries (DLL) created by QT6, the main program will crash:

            My dll file was built with QT6, and then I created another project with QT6 and tried to call it

            You don't "call" DLLs, you link them.

            Then I added the generated file to my project directory, but it's still the same.

            This is not what @J-Hilk was talking about.
            If you export/deploy your qt widget library, you need to ship it with all the other libs your library needs.

            @pedisChen said in When calling the Shared Libraries (DLL) created by QT6, the main program will crash:

            I think it is because the dll lacks of an QT event loop mechanism. so I added an init function in dll,and need to call the init function first , defined like this:

            QCoreApplication a(argc, argv);
            return a.exec();
            

            A library doesn't necessarily need that.
            A Qt library can be a QWidget, just a colletion of functions or something else...
            None of these need their own QCoreApplication, when used in other Qt apps.
            (They do in general in order to work, but at least not their own. The main loop from the app to which your library is linked to, is fine).

            See this example:
            If you look at the code, there is no main.cpp or additional QApplication involved.
            But one is needed to use the library widget in your main app.

            • https://doc.qt.io/qt-6/qtdesigner-customwidgetplugin-example.html
              • Code: https://code.qt.io/cgit/qt/qttools.git/tree/examples/designer/customwidgetplugin?h=6.8

            Edit:

            @pedisChen said in When calling the Shared Libraries (DLL) created by QT6, the main program will crash:

            This worked, but it would always block the main program. Then I planned to use QThread to create a new process in the dll to run a.exec(), but it failed and the new process task could not run.I dont know why...

            In what way do you use your library? Or how do you intend to use it in some application?
            How does your library work? Do you export a single widget, which uses QTCPSocket in your library?

            Another observation:

            If you don't have an event loop in your main application (when no Qt involved in main app), you obviously can't create a QThread which then should host the QApplication instance... QThread is a wrapper for std::thread and is a QObject itself. Therefore it also needs a QApplication to work properly.
            Use a plain std::thread instead.


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            pedisChenP 1 Reply Last reply
            3
            • Pl45m4P Pl45m4

              @pedisChen said in When calling the Shared Libraries (DLL) created by QT6, the main program will crash:

              My dll file was built with QT6, and then I created another project with QT6 and tried to call it

              You don't "call" DLLs, you link them.

              Then I added the generated file to my project directory, but it's still the same.

              This is not what @J-Hilk was talking about.
              If you export/deploy your qt widget library, you need to ship it with all the other libs your library needs.

              @pedisChen said in When calling the Shared Libraries (DLL) created by QT6, the main program will crash:

              I think it is because the dll lacks of an QT event loop mechanism. so I added an init function in dll,and need to call the init function first , defined like this:

              QCoreApplication a(argc, argv);
              return a.exec();
              

              A library doesn't necessarily need that.
              A Qt library can be a QWidget, just a colletion of functions or something else...
              None of these need their own QCoreApplication, when used in other Qt apps.
              (They do in general in order to work, but at least not their own. The main loop from the app to which your library is linked to, is fine).

              See this example:
              If you look at the code, there is no main.cpp or additional QApplication involved.
              But one is needed to use the library widget in your main app.

              • https://doc.qt.io/qt-6/qtdesigner-customwidgetplugin-example.html
                • Code: https://code.qt.io/cgit/qt/qttools.git/tree/examples/designer/customwidgetplugin?h=6.8

              Edit:

              @pedisChen said in When calling the Shared Libraries (DLL) created by QT6, the main program will crash:

              This worked, but it would always block the main program. Then I planned to use QThread to create a new process in the dll to run a.exec(), but it failed and the new process task could not run.I dont know why...

              In what way do you use your library? Or how do you intend to use it in some application?
              How does your library work? Do you export a single widget, which uses QTCPSocket in your library?

              Another observation:

              If you don't have an event loop in your main application (when no Qt involved in main app), you obviously can't create a QThread which then should host the QApplication instance... QThread is a wrapper for std::thread and is a QObject itself. Therefore it also needs a QApplication to work properly.
              Use a plain std::thread instead.

              pedisChenP Offline
              pedisChenP Offline
              pedisChen
              wrote on last edited by
              #6

              @Pl45m4 said in When calling the Shared Libraries (DLL) created by QT6, the main program will crash:

              If you export/deploy your qt widget library, you need to ship it with all the other libs your library needs.

              Because I was linking the dll file use another qt project
              so I copied all the files(ship it all?) generated by windeployqt6.exe to the project dlll linking dir

              I use QTCPSocket in the dll for TCP connection, and then the main program crashes for unknown reasons

              dll code like this:
              A.cpp

              uint8_t mySocket::Connect()
              {
                  if(this->ip.isEmpty()) return CONNECT_ADDR_FAIL;
                  if(this->port == 0) return CONNECT_ADDR_FAIL;
              
                  /* device connect */
                  if(_s.state() != QAbstractSocket::ConnectedState)
                      _s.connectToHost(this->ip, this->port);
                  return 0;
              }
              

              B.cpp

              uint8_t tcp_connect(void *mySocketHandle)
              {
                  return ((mySockeDLL_t)mySocketHandle)->Connect();
              }
              

              B.h

              #ifdef __cplusplus
              extern "C"
              {
              #endif //__cplusplus
              ....
              uint8_t DLL_EXPORT tcp_connect(void * mySocketHandle);
              
              .....
              #ifdef __cplusplus
              }
              #endif //__cplusplus
              

              in another project, use QLibrary to link dlls
              code like this

                  QLibrary library("myDLL");
              
                  if (library.load())  
                  {
              
                       ......
                      typedef uint8_t (* tcp_connect)(void * mySocketHandle);
                      tcp_connect f2 = (tcp_connect)library.resolve("tcp_connect");
                      f2(mySocketHandle);
                       .......
                  }
                  library.unload();
              

              after run .exe
              23:00:52: D:\Code\QT_PROJECT\libraryTest\importDLL\build-importDLL-Desktop_Qt_6_5_3_MinGW_64_bit-Release\release\importDLL.exe crashed.

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by Christian Ehrlicher
                #7

                Apart from wtf are you doing here:

                f2(mySocketHandle);

                Does mySocketHandle really contains the pointer to your class instance?

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                pedisChenP 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  Apart from wtf are you doing here:

                  f2(mySocketHandle);

                  Does mySocketHandle really contains the pointer to your class instance?

                  pedisChenP Offline
                  pedisChenP Offline
                  pedisChen
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher yes,it is a pointer,maybe the var name is confusing,but the tcp socket connected successfully and after some second the main program crash down

                  JonBJ 1 Reply Last reply
                  0
                  • pedisChenP pedisChen

                    @Christian-Ehrlicher yes,it is a pointer,maybe the var name is confusing,but the tcp socket connected successfully and after some second the main program crash down

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

                    @pedisChen Run under debugger and see whether stack trace gives you a hint.

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      tbh - wrapping a c++ class in a c api is...

                      Why all this strange stuff instead simply linking against the library like everyone else is doing it?

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      pedisChenP 1 Reply Last reply
                      1
                      • Christian EhrlicherC Christian Ehrlicher

                        tbh - wrapping a c++ class in a c api is...

                        Why all this strange stuff instead simply linking against the library like everyone else is doing it?

                        pedisChenP Offline
                        pedisChenP Offline
                        pedisChen
                        wrote on last edited by
                        #11

                        @Christian-Ehrlicher because Idont know how to use Qlibrary to resolve a class object and I want it to be compatible for use in code written in C.

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by Christian Ehrlicher
                          #12

                          because Idont know how to use Qlibrary to resolve a class object

                          https://doc.qt.io/qt-6/plugins-howto.html#the-low-level-api-extending-qt-applications

                          and I want it to be compatible for use in code written in C.

                          Since you need a running Q(Core)Application somewhere this will not work out.

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          pedisChenP 2 Replies Last reply
                          4
                          • Christian EhrlicherC Christian Ehrlicher

                            because Idont know how to use Qlibrary to resolve a class object

                            https://doc.qt.io/qt-6/plugins-howto.html#the-low-level-api-extending-qt-applications

                            and I want it to be compatible for use in code written in C.

                            Since you need a running Q(Core)Application somewhere this will not work out.

                            pedisChenP Offline
                            pedisChenP Offline
                            pedisChen
                            wrote on last edited by
                            #13

                            @Christian-Ehrlicher said in When calling the Shared Libraries (DLL) created by QT6, the main program will crash:

                            Since you need a running Q(Core)Application somewhere this will not work out.

                            Can I still use QTcpSocket for TCP communication? I want the generated DLL to be linkable by C code.

                            Christian EhrlicherC 1 Reply Last reply
                            0
                            • Christian EhrlicherC Christian Ehrlicher

                              because Idont know how to use Qlibrary to resolve a class object

                              https://doc.qt.io/qt-6/plugins-howto.html#the-low-level-api-extending-qt-applications

                              and I want it to be compatible for use in code written in C.

                              Since you need a running Q(Core)Application somewhere this will not work out.

                              pedisChenP Offline
                              pedisChenP Offline
                              pedisChen
                              wrote on last edited by
                              #14

                              @Christian-Ehrlicher Can I create a init function in dll running in another thread that running QCoreApplication?

                              1 Reply Last reply
                              0
                              • pedisChenP pedisChen

                                @Christian-Ehrlicher said in When calling the Shared Libraries (DLL) created by QT6, the main program will crash:

                                Since you need a running Q(Core)Application somewhere this will not work out.

                                Can I still use QTcpSocket for TCP communication? I want the generated DLL to be linkable by C code.

                                Christian EhrlicherC Offline
                                Christian EhrlicherC Offline
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @pedisChen said in When calling the Shared Libraries (DLL) created by QT6, the main program will crash:

                                Can I still use QTcpSocket for TCP communication? I want the generated DLL to be linkable by C code.

                                You need a running Q(Core)Application as we already told you.

                                Can I create a init function in dll running in another thread that running QCoreApplication?

                                You can, but you will get in trouble when the objects are not in that thread. It's not worth the trouble.

                                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                Visit the Qt Academy at https://academy.qt.io/catalog

                                pedisChenP 1 Reply Last reply
                                0
                                • Christian EhrlicherC Christian Ehrlicher

                                  @pedisChen said in When calling the Shared Libraries (DLL) created by QT6, the main program will crash:

                                  Can I still use QTcpSocket for TCP communication? I want the generated DLL to be linkable by C code.

                                  You need a running Q(Core)Application as we already told you.

                                  Can I create a init function in dll running in another thread that running QCoreApplication?

                                  You can, but you will get in trouble when the objects are not in that thread. It's not worth the trouble.

                                  pedisChenP Offline
                                  pedisChenP Offline
                                  pedisChen
                                  wrote on last edited by
                                  #16

                                  @Christian-Ehrlicher said in When calling the Shared Libraries (DLL) created by QT6, the main program will crash:

                                  You can, but you will get in trouble when the objects are not in that thread. It's not worth the trouble.

                                  But if it's in the same thread, QCoreApplication will block the program from running. How should I solve this?

                                  Pl45m4P 1 Reply Last reply
                                  0
                                  • pedisChenP pedisChen

                                    @Christian-Ehrlicher said in When calling the Shared Libraries (DLL) created by QT6, the main program will crash:

                                    You can, but you will get in trouble when the objects are not in that thread. It's not worth the trouble.

                                    But if it's in the same thread, QCoreApplication will block the program from running. How should I solve this?

                                    Pl45m4P Offline
                                    Pl45m4P Offline
                                    Pl45m4
                                    wrote on last edited by
                                    #17

                                    @pedisChen said in When calling the Shared Libraries (DLL) created by QT6, the main program will crash:

                                    But if it's in the same thread, QCoreApplication will block the program from running. How should I solve this?

                                    Like I've said above, create a new std::thread for your Q(Core)Application.


                                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                    ~E. W. Dijkstra

                                    pedisChenP 1 Reply Last reply
                                    0
                                    • Pl45m4P Pl45m4

                                      @pedisChen said in When calling the Shared Libraries (DLL) created by QT6, the main program will crash:

                                      But if it's in the same thread, QCoreApplication will block the program from running. How should I solve this?

                                      Like I've said above, create a new std::thread for your Q(Core)Application.

                                      pedisChenP Offline
                                      pedisChenP Offline
                                      pedisChen
                                      wrote on last edited by
                                      #18

                                      @Pl45m4 Thank you for your reply. I tried. here is my example code:

                                      Version A

                                      void start_app(void)
                                      {
                                          int argc = 0;
                                          char*argv[] = {nullptr};
                                          QCoreApplication a(argc, argv);
                                      
                                          a.exec();
                                      }
                                      
                                      void init_dll()
                                      {
                                          std::thread th(start_app);
                                          h.detach();
                                      }
                                      

                                      I use QLibrary to resolve "init_dll" function and run it.This time the main program is not blocked, no crash down occured .But the signal slot did not work, and QTCPSocket cannot be used.

                                      Version B

                                      void start_app(void)
                                      {
                                          int argc = 0;
                                          char*argv[] = {nullptr};
                                          QCoreApplication a(argc, argv);
                                      
                                          a.exec();
                                      }
                                      
                                      void init_dll()
                                      {
                                          start_app();
                                      }
                                      

                                      when I call "init_dll" function in DLLs,the main program will be blocked from running, but the QTCPSocket and signal slots function is workable.

                                      How can I modify the DLL code so it doesn't block the main program's execution and still allows using QTcpSocket and other Qt components that depend on the event loop(QCoreApplication)?

                                      1 Reply Last reply
                                      0
                                      • pedisChenP pedisChen has marked this topic as solved on
                                      • pedisChenP pedisChen has marked this topic as solved on
                                      • Pl45m4P Pl45m4 referenced this topic on

                                      • Login

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