[Solved] Loading a dll at run time?
-
wrote on 9 Mar 2014, 17:01 last edited by
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? -
wrote on 10 Mar 2014, 04:57 last edited by
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 -
wrote on 10 Mar 2014, 13:46 last edited by
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? -
wrote on 10 Mar 2014, 17:03 last edited by
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. -
wrote on 11 Mar 2014, 04:27 last edited by
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.
-
wrote on 11 Mar 2014, 15:46 last edited by
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;
}@
-
wrote on 12 Mar 2014, 04:18 last edited by
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 -
wrote on 12 Mar 2014, 14:37 last edited by
@qxoz Thanks for your valuable advice!
1/8