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. [Solved] Loading a dll at run time?
QtWS25 Last Chance

[Solved] Loading a dll at run time?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 3.3k 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.
  • CAD_codingC Offline
    CAD_codingC Offline
    CAD_coding
    wrote on last edited by
    #1

    I have a dll (A.dll) which needs to be loaded at run time because it might not be present on all PC's.
    I tried using QLibrary but it gave me strange segmentation faults.
    So now I feel QPluginLoader can do the same thing for me, how?

    I am static linking the A.lib (.lib for A.dll) in another project which creates a shared library say B.dll.
    Now by QFile I will first check if A.dll is present on the PC and if yes I will load B.dll by QPluginLoader.
    I want to know whether this approach is correct or not?

    Additionally I am not able to find enough tutorials on QPLuginLoader on the internet.
    Can you provide any beginner level tutorial on this topic?

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      Hi CAD_coding, i hope link below will be helpful
      "tutorial":http://voidrealms.com/index.php?r=tutorial/view&id=286
      this tutorial shows how create plugins for Qt4 based project. Plugin creation for Qt5 little different, but you can find out it in documentation. Also look at
      "Qt5.0:How to Create Qt Plugins":http://qt-project.org/doc/qt-5.0/qtcore/plugins-howto.html
      "Qt4:How to Create Qt Plugins":http://qt-project.org/doc/qt-4.8/plugins-howto.html

      1 Reply Last reply
      0
      • CAD_codingC Offline
        CAD_codingC Offline
        CAD_coding
        wrote on last edited by
        #3

        Hello qxoz,

        Thank You for providing your help.
        The wiki page is extremely helpful!

        I wanted your suggestion on this matter though.
        As mentioned in the question the plugin (dll) that I will load is actually statically linked with another system dll. However this system dll may not be present on every PC. So if it is not present then I will not load the plugin.
        Just wanted to know whether there is any problem with my approach?

        1 Reply Last reply
        0
        • CAD_codingC Offline
          CAD_codingC Offline
          CAD_coding
          wrote on last edited by
          #4

          Hi qxoz,
          I am trying to build the plugin using your links.
          I dont know why but the plugin dll when created in release mode has '1' added to its name while in debug mode it is correct.
          For example if the plugin target is "my.dll" in debug mode then in release mode it is "my1.dll". Why is this happening?
          If I rename it to remove the 1 then my application is able to load the plugin.

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qxoz
            wrote on last edited by
            #5

            Yes it's ok. Probably you add VERSION = 1 in to your *.pro file. If you don't want extend your dll with version number just remove VERSION variable.

            1 Reply Last reply
            0
            • CAD_codingC Offline
              CAD_codingC Offline
              CAD_coding
              wrote on last edited by
              #6

              Yes I removed the VERSION from my pro file and the name became normal again. I have prepared a bare minimum function which checks whether the plugin can be loaded or not. It is working as I want but I am concerned whether I am undloading the plugin properly and also memory management is correct?

              @bool OpenCLDevices::canLoadOpenCLPlugin()
              {
              bool result = false;
              {
              QLibrary lib("OpenCL");
              if(lib.load())
              {
              lib.unload();

                      QString path = QApplication::arguments().at(0);
                      path = path.replace("\\\\", "\\").replace("\\", "/");
                      path.truncate(path.lastIndexOf('/'));
                      path.append("/SeqOpenCL.");
                      if(UserPrefs::getOS() == "Windows_NT")
                          path.append("dll");
                      else
                          path.append("so");
                      QPluginLoader loader(path);
                      QObject *possiblePlugin = loader.instance();
                      if(possiblePlugin)
                      {
                          PluginInterface *plugin = qobject_cast <PluginInterface *>(possiblePlugin);
                          if(plugin)
                              result = true;
                      }
                      loader.unload();
                  }
              }
              
              return result;
              

              }@

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                qxoz
                wrote on last edited by
                #7

                As documentation says:
                [quote]bool QLibrary::unload()
                Unloads the library and returns true if the library could be unloaded; otherwise returns false.
                This happens automatically on application termination, so you shouldn’t normally need to call this function. [/quote]
                So i think you don’t need call unload for qpluginloader or qlibrary.
                also take a look to examples:
                "Echo Plugin Example":https://qt-project.org/doc/qt-5/qtwidgets-tools-echoplugin-example.html
                "Plug & Paint Example":https://qt-project.org/doc/qt-5/qtwidgets-tools-plugandpaint-example.html

                1 Reply Last reply
                0
                • CAD_codingC Offline
                  CAD_codingC Offline
                  CAD_coding
                  wrote on last edited by
                  #8

                  @qxoz Thanks for your valuable advice!

                  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