Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. QT5 + DLL loader (run-time)
Forum Updated to NodeBB v4.3 + New Features

QT5 + DLL loader (run-time)

Scheduled Pinned Locked Moved Solved 3rd Party Software
19 Posts 4 Posters 4.3k 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.
  • F fem_dev

    @sgaist thanks for your quick response!

    Could you please give me a short example about how to call a internal DLL function?

    I tried some code like this:

    QPluginLoader loader("my_dll.dll");
    QObject* plugin = loader.instance();
    
    if (plugin)
    {
        // how to call a internal DLL function?
    }
    

    Inside my *.pro file, I added:

    LIBS = -L../plugins
    

    And finally, I copied my dll file to the QT project build folder:

    C:\full_path_to_build_folder\plugins\my_dll.dll
    

    Inside my_dll.dll I have 2 functions:

    void sum(int *a, int *b, int* c);
    void sub(int *a, int *b, int* c);
    

    Could you help me to call these functions?

    F Offline
    F Offline
    fem_dev
    wrote on last edited by fem_dev
    #4

    I got this code working well. It calls the "sub" function that is inside my DLL file.

    int a = 5;
    int b = 3;
    int c = 0;
    
    // Load library in a cross-platform way: Windows (*.dll), Linux (*.so) or Mac (*.dylib)
    QLibrary myLib("Dll1"); 
    typedef void (*MyPrototype)(int*, int*, int*);
    MyPrototype sub = (MyPrototype) myLib.resolve("sub");
    if (sub) {
        sub(&a, &b, &c);
    }
    
    qDebug() << "C = " << c; // Output: c = 2
    

    Question 1:
    In the line:

    MyPrototype sub = (MyPrototype) myLib.resolve("sub");
    

    I got this warning:

    Use of old-style cast
    

    How to do a "new-style cast" and remove this warning?

    Question 2:
    What is MyPrototype in this code? Why it works?

    Question 3:
    Can I put QT GUI elements (Window, buttons, labels, etc...) inside a DLL file?

    aha_1980A 1 Reply Last reply
    0
    • F fem_dev

      I got this code working well. It calls the "sub" function that is inside my DLL file.

      int a = 5;
      int b = 3;
      int c = 0;
      
      // Load library in a cross-platform way: Windows (*.dll), Linux (*.so) or Mac (*.dylib)
      QLibrary myLib("Dll1"); 
      typedef void (*MyPrototype)(int*, int*, int*);
      MyPrototype sub = (MyPrototype) myLib.resolve("sub");
      if (sub) {
          sub(&a, &b, &c);
      }
      
      qDebug() << "C = " << c; // Output: c = 2
      

      Question 1:
      In the line:

      MyPrototype sub = (MyPrototype) myLib.resolve("sub");
      

      I got this warning:

      Use of old-style cast
      

      How to do a "new-style cast" and remove this warning?

      Question 2:
      What is MyPrototype in this code? Why it works?

      Question 3:
      Can I put QT GUI elements (Window, buttons, labels, etc...) inside a DLL file?

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by aha_1980
      #5

      Hi @fem_dev said in QT5 + DLL loader (run-time):

      Question 1:
      In the line:
      MyPrototype sub = (MyPrototype) myLib.resolve("sub");

      I got this warning:
      Use of old-style cast

      How to do a "new-style cast" and remove this warning?

      MyPrototype sub = static_cast<MyPrototype>(myLib.resolve("sub"));

      Question 2:
      What is MyPrototype in this code? Why it works?

      You defined it yourself: typedef void (*MyPrototype)(int*, int*, int*);. This is just the signature of the function you are calling: a void function taking three pointers to int as parameter. Note that you are defining a pointer to this function, because that is what myLib.resolve() returns.

      Question 3:
      Can I put QT GUI elements (Window, buttons, labels, etc...) inside a DLL file?

      Yes, but creating a real plugin with QPluginLoader is better suited for that.

      Regards

      Qt has to stay free or it will die.

      F 3 Replies Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #6

        The idea behind plugins is that you create one or more interfaces where you define what your plugin provides. You call then these methods as you need.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • aha_1980A aha_1980

          Hi @fem_dev said in QT5 + DLL loader (run-time):

          Question 1:
          In the line:
          MyPrototype sub = (MyPrototype) myLib.resolve("sub");

          I got this warning:
          Use of old-style cast

          How to do a "new-style cast" and remove this warning?

          MyPrototype sub = static_cast<MyPrototype>(myLib.resolve("sub"));

          Question 2:
          What is MyPrototype in this code? Why it works?

          You defined it yourself: typedef void (*MyPrototype)(int*, int*, int*);. This is just the signature of the function you are calling: a void function taking three pointers to int as parameter. Note that you are defining a pointer to this function, because that is what myLib.resolve() returns.

          Question 3:
          Can I put QT GUI elements (Window, buttons, labels, etc...) inside a DLL file?

          Yes, but creating a real plugin with QPluginLoader is better suited for that.

          Regards

          F Offline
          F Offline
          fem_dev
          wrote on last edited by
          #7

          @aha_1980 thank you for your good explanation.

          Please, could you say to me what is the difference between QPluginLoader and QLibrary.
          Reading the QT Documentation, I saw that QPluginLoader is only for static plugins, but the wierd part is that they are loaded in run-time. I don't understood this very well.

          In my mind, the word "Static" means a library that will be placed inside the main application binary. And "static" means too that the file extension is ".lib" (Windows) or .a (Linux). Right?

          My goal is to build a main application that can load QT plugins at run-time and, of course, they are not inside the main application binary.

          So, what the difference about these two approachs: QPluginLoader and QLibrary?

          aha_1980A 1 Reply Last reply
          0
          • F fem_dev

            @aha_1980 thank you for your good explanation.

            Please, could you say to me what is the difference between QPluginLoader and QLibrary.
            Reading the QT Documentation, I saw that QPluginLoader is only for static plugins, but the wierd part is that they are loaded in run-time. I don't understood this very well.

            In my mind, the word "Static" means a library that will be placed inside the main application binary. And "static" means too that the file extension is ".lib" (Windows) or .a (Linux). Right?

            My goal is to build a main application that can load QT plugins at run-time and, of course, they are not inside the main application binary.

            So, what the difference about these two approachs: QPluginLoader and QLibrary?

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #8

            @fem_dev said in QT5 + DLL loader (run-time):

            Reading the QT Documentation, I saw that QPluginLoader is only for static plugins, but the wierd part is that they are loaded in run-time. I don't understood this very well.

            AFAIK you can load plugins at runtime. At least they are not linked into your program, but rather external libraries. Qt Plugins are very well suited for Qt code that extends your main program.

            In my mind, the word "Static" means a library that will be placed inside the main application binary. And "static" means too that the file extension is ".lib" (Windows) or .a (Linux). Right?

            No, the extension is .dll or .so. For example, Qt Creator is fully made of plugins; you can look in the bin or libfolder and you will see all the plugins.

            I use QLibrary myself to load external libraries (not written by me). Your Fortran code might be perfect for that.

            I hope I could clarify that a bit, I suggest that you have a deeper look at the Plug&Paint example and play a bit with it.

            Qt has to stay free or it will die.

            F 1 Reply Last reply
            1
            • aha_1980A aha_1980

              @fem_dev said in QT5 + DLL loader (run-time):

              Reading the QT Documentation, I saw that QPluginLoader is only for static plugins, but the wierd part is that they are loaded in run-time. I don't understood this very well.

              AFAIK you can load plugins at runtime. At least they are not linked into your program, but rather external libraries. Qt Plugins are very well suited for Qt code that extends your main program.

              In my mind, the word "Static" means a library that will be placed inside the main application binary. And "static" means too that the file extension is ".lib" (Windows) or .a (Linux). Right?

              No, the extension is .dll or .so. For example, Qt Creator is fully made of plugins; you can look in the bin or libfolder and you will see all the plugins.

              I use QLibrary myself to load external libraries (not written by me). Your Fortran code might be perfect for that.

              I hope I could clarify that a bit, I suggest that you have a deeper look at the Plug&Paint example and play a bit with it.

              F Offline
              F Offline
              fem_dev
              wrote on last edited by
              #9

              @aha_1980 thanks for your quick response again and your good clarification about this topic.

              Well, I saw the Plug&Paint (the main application part) and Plug&Paint Basic Tools (the Plugins part) QT examples...
              The main application is using QPluginLoader to load the "Basic Tools" static library plugins. Ok!

              In this QT example, the Plug&Paint application NOT compiles before compile the "Basic Tools" QT Project. I think that's because the "Basic Tools" output is a static library, and this static library (*.lib) is dependence library of Plug&Paint project.

              So, first of all, I need to compile "Basic Tools" project -> so, I get the output static lib file -> and then compile "Plug&Paint" main application.

              My goal is different: I would like to develop a QT main application that doesn't have any "plugin dependencies" at compile time.
              The plugins must extend the main application only in run-time.

              My future plugins should be a mix of "Fortran + QT GUI + C++".

              Question: 1
              Is there a "good practice" to develop this compile-time independent "main application"?
              May be examples, tutoriais, etc...

              Question 2:
              What is the best "QT Creator project type" to develop this GUI plugin? Why?
              a) QT Plugin?
              b) Dynamic Lib?
              c) Static Lib?
              d) Other?

              Question 3:
              What is the "best way" to load these plugins in run-time? Why?
              a) Using QPluginLoader?
              b) Using QLibrary?

              aha_1980A 1 Reply Last reply
              0
              • F fem_dev

                @aha_1980 thanks for your quick response again and your good clarification about this topic.

                Well, I saw the Plug&Paint (the main application part) and Plug&Paint Basic Tools (the Plugins part) QT examples...
                The main application is using QPluginLoader to load the "Basic Tools" static library plugins. Ok!

                In this QT example, the Plug&Paint application NOT compiles before compile the "Basic Tools" QT Project. I think that's because the "Basic Tools" output is a static library, and this static library (*.lib) is dependence library of Plug&Paint project.

                So, first of all, I need to compile "Basic Tools" project -> so, I get the output static lib file -> and then compile "Plug&Paint" main application.

                My goal is different: I would like to develop a QT main application that doesn't have any "plugin dependencies" at compile time.
                The plugins must extend the main application only in run-time.

                My future plugins should be a mix of "Fortran + QT GUI + C++".

                Question: 1
                Is there a "good practice" to develop this compile-time independent "main application"?
                May be examples, tutoriais, etc...

                Question 2:
                What is the best "QT Creator project type" to develop this GUI plugin? Why?
                a) QT Plugin?
                b) Dynamic Lib?
                c) Static Lib?
                d) Other?

                Question 3:
                What is the "best way" to load these plugins in run-time? Why?
                a) Using QPluginLoader?
                b) Using QLibrary?

                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by
                #10

                @fem_dev I suggest you to do a bit of information research yourself. As said, playing with existing examples also helps to understand how things work.

                Me and @SGaist already gave examples how you could start your work.

                Once again: plugins can be compiled separate from your app and can be loaded at runtime. That's what QPluginLoader helps you to do.

                QLibrary is for loading external libraries, e.g. your pure FORTRAN libs.

                I really cannot tell you more, because I've used QLibrary a lot, but did not much with plugins so far.

                Regards

                Qt has to stay free or it will die.

                1 Reply Last reply
                3
                • hskoglundH Offline
                  hskoglundH Offline
                  hskoglund
                  wrote on last edited by
                  #11

                  Hi, to add to @aha_1980: I've used both QLibrary and QPluginLoader, you could say QPluginLoader is a superset of QLibrary. I haven't looked at the source code, but QPluginLoader probably uses QLibrary to load the its plugin .dlls.

                  They're doing the same basic job, but QPluginLoader does much more checking and tidying up, so wiring up/talking to your .dlls in your app is easier going through QPluginLoader.

                  IOW, QPluginLoader is more high-level, like an automatic car, while QLibrary is more like driving a stick-shift car :-)

                  1 Reply Last reply
                  4
                  • F Offline
                    F Offline
                    fem_dev
                    wrote on last edited by
                    #12

                    Thank you so much @aha_1980 , @SGaist and @hskoglund ....now I'm getting the main idea about QPluginLoader and QLibrary. You guys help me a lot!

                    I will try to do a simple code here.

                    All the best!

                    1 Reply Last reply
                    1
                    • aha_1980A aha_1980

                      Hi @fem_dev said in QT5 + DLL loader (run-time):

                      Question 1:
                      In the line:
                      MyPrototype sub = (MyPrototype) myLib.resolve("sub");

                      I got this warning:
                      Use of old-style cast

                      How to do a "new-style cast" and remove this warning?

                      MyPrototype sub = static_cast<MyPrototype>(myLib.resolve("sub"));

                      Question 2:
                      What is MyPrototype in this code? Why it works?

                      You defined it yourself: typedef void (*MyPrototype)(int*, int*, int*);. This is just the signature of the function you are calling: a void function taking three pointers to int as parameter. Note that you are defining a pointer to this function, because that is what myLib.resolve() returns.

                      Question 3:
                      Can I put QT GUI elements (Window, buttons, labels, etc...) inside a DLL file?

                      Yes, but creating a real plugin with QPluginLoader is better suited for that.

                      Regards

                      F Offline
                      F Offline
                      fem_dev
                      wrote on last edited by
                      #13

                      @aha_1980 this is the last doubt in this post:

                      When I try to follow your sugestion replacing this line:

                      MyPrototype sub = (MyPrototype) myLib.resolve("sub");

                      To:

                      MyPrototype sub = static_cast<MyPrototype>(myLib.resolve("sub"));

                      I got this compilation error:

                      error: static_cast from 'QFunctionPointer' (aka 'void (*)()') to 'MyPrototype' (aka 'void (*)(int *, int *, int *)') is not allowed

                      Coul you help me?

                      Thank you,

                      aha_1980A 1 Reply Last reply
                      0
                      • F fem_dev

                        @aha_1980 this is the last doubt in this post:

                        When I try to follow your sugestion replacing this line:

                        MyPrototype sub = (MyPrototype) myLib.resolve("sub");

                        To:

                        MyPrototype sub = static_cast<MyPrototype>(myLib.resolve("sub"));

                        I got this compilation error:

                        error: static_cast from 'QFunctionPointer' (aka 'void (*)()') to 'MyPrototype' (aka 'void (*)(int *, int *, int *)') is not allowed

                        Coul you help me?

                        Thank you,

                        aha_1980A Offline
                        aha_1980A Offline
                        aha_1980
                        Lifetime Qt Champion
                        wrote on last edited by
                        #14

                        @fem_dev

                        I've no compiler at hand now, but I thought that should work...

                        Qt has to stay free or it will die.

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by SGaist
                          #15

                          What is MyPrototype exactly ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • aha_1980A aha_1980

                            Hi @fem_dev said in QT5 + DLL loader (run-time):

                            Question 1:
                            In the line:
                            MyPrototype sub = (MyPrototype) myLib.resolve("sub");

                            I got this warning:
                            Use of old-style cast

                            How to do a "new-style cast" and remove this warning?

                            MyPrototype sub = static_cast<MyPrototype>(myLib.resolve("sub"));

                            Question 2:
                            What is MyPrototype in this code? Why it works?

                            You defined it yourself: typedef void (*MyPrototype)(int*, int*, int*);. This is just the signature of the function you are calling: a void function taking three pointers to int as parameter. Note that you are defining a pointer to this function, because that is what myLib.resolve() returns.

                            Question 3:
                            Can I put QT GUI elements (Window, buttons, labels, etc...) inside a DLL file?

                            Yes, but creating a real plugin with QPluginLoader is better suited for that.

                            Regards

                            F Offline
                            F Offline
                            fem_dev
                            wrote on last edited by
                            #16

                            @aha_1980 said in QT5 + DLL loader (run-time):

                            Hi @fem_dev said in QT5 + DLL loader (run-time):

                            Question 1:
                            In the line:
                            MyPrototype sub = (MyPrototype) myLib.resolve("sub");

                            I got this warning:
                            Use of old-style cast

                            How to do a "new-style cast" and remove this warning?

                            MyPrototype sub = static_cast<MyPrototype>(myLib.resolve("sub"));

                            Question 2:
                            What is MyPrototype in this code? Why it works?

                            You defined it yourself: typedef void (*MyPrototype)(int*, int*, int*);. This is just the signature of the function you are calling: a void function taking three pointers to int as parameter. Note that you are defining a pointer to this function, because that is what myLib.resolve() returns.

                            Question 3:
                            Can I put QT GUI elements (Window, buttons, labels, etc...) inside a DLL file?

                            Yes, but creating a real plugin with QPluginLoader is better suited for that.

                            Regards

                            aha_1980A 1 Reply Last reply
                            0
                            • F fem_dev

                              @aha_1980 said in QT5 + DLL loader (run-time):

                              Hi @fem_dev said in QT5 + DLL loader (run-time):

                              Question 1:
                              In the line:
                              MyPrototype sub = (MyPrototype) myLib.resolve("sub");

                              I got this warning:
                              Use of old-style cast

                              How to do a "new-style cast" and remove this warning?

                              MyPrototype sub = static_cast<MyPrototype>(myLib.resolve("sub"));

                              Question 2:
                              What is MyPrototype in this code? Why it works?

                              You defined it yourself: typedef void (*MyPrototype)(int*, int*, int*);. This is just the signature of the function you are calling: a void function taking three pointers to int as parameter. Note that you are defining a pointer to this function, because that is what myLib.resolve() returns.

                              Question 3:
                              Can I put QT GUI elements (Window, buttons, labels, etc...) inside a DLL file?

                              Yes, but creating a real plugin with QPluginLoader is better suited for that.

                              Regards

                              aha_1980A Offline
                              aha_1980A Offline
                              aha_1980
                              Lifetime Qt Champion
                              wrote on last edited by
                              #17

                              @fem_dev

                              Please try this:

                              MyPrototype sub = reinterpret_cast<MyPrototype>(myLib.resolve("sub"));

                              I'm pretty sure that will work.

                              Qt has to stay free or it will die.

                              F 1 Reply Last reply
                              2
                              • aha_1980A aha_1980

                                @fem_dev

                                Please try this:

                                MyPrototype sub = reinterpret_cast<MyPrototype>(myLib.resolve("sub"));

                                I'm pretty sure that will work.

                                F Offline
                                F Offline
                                fem_dev
                                wrote on last edited by
                                #18

                                @aha_1980 thank you so much!
                                It works!!

                                @aha_1980 you are the best!

                                Thank you again!

                                aha_1980A 1 Reply Last reply
                                1
                                • F fem_dev

                                  @aha_1980 thank you so much!
                                  It works!!

                                  @aha_1980 you are the best!

                                  Thank you again!

                                  aha_1980A Offline
                                  aha_1980A Offline
                                  aha_1980
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #19

                                  Hi @fem_dev,

                                  I'm flattered, and glad I could help you.

                                  Regards

                                  Qt has to stay free or it will die.

                                  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