[Solved] Loading a dll at run time?

  • 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?

  • Hi CAD_coding, i hope link below will be helpful
    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

  • 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?

  • 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.

  • 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.

  • 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");

            QString path = QApplication::arguments().at(0);
            path = path.replace("\\\\", "\\").replace("\\", "/");
            if(UserPrefs::getOS() == "Windows_NT")
            QPluginLoader loader(path);
            QObject *possiblePlugin = loader.instance();
                PluginInterface *plugin = qobject_cast <PluginInterface *>(possiblePlugin);
                    result = true;
    return result;


  • 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

  • @qxoz Thanks for your valuable advice!

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.