2

Suppose I have two libraries, A.dll and B.dll. The library A depends on B. I want to load A into my project.

My project is in C:/Project. If I keep both A.dll and B.dll in C:/Project, I can load A with:

QLibrary lib("A"); 
lib.load();

This works fine. load() will return false if B.dll isn't in C:/Project, though.

The problem is I want to keep both A.dll and B.dll in C:/Project/lib. But when I move both libs to that location and try to load:

QLibrary lib("C:/Project/lib/A");
lib.load();

It fails. But this works if I keep A.dll in C:/Project/lib and B.dll in C:/Project.

How can I keep both libs in C:/Project/liband have A.dll load successfully?

Edit: the error message I get is "Cannot load library C:/Project/lib/A . The specified module could not be found."

liewl
  • 4,021
  • 13
  • 46
  • 65
  • Could you please print out the error string by << lib.errorString()` and then share it with us? – László Papp Mar 13 '14 at 03:42
  • Sure, just updated the question with it. – liewl Mar 13 '14 at 11:40
  • 1
    I think `QLibrary lib("C:/Project/lib/A");` may be in appropriate. I would personally keep the `QLibrary lib("A");` original version, and put the "C:\Project\lib\" into the search path. Can you try that please and let me know the result? You could try `QCoreApplication::addLibraryPath(const QString& path)` where the path is the aforementioned one. – László Papp Mar 13 '14 at 11:48
  • It worked. I used SetDllDirectory() as DmitryARN recommended, and kept QLibrary lib("A");. – liewl Mar 13 '14 at 12:55
  • Please also try the Qt solution and let me know if it works. It is not ideal to use Windows specific API in a Qt (cross-platform) application if there are qt alternatives for that API. This way, you could reduce the platform specific detail to a string, and not API, which would be a neater solution in the Qt world IMHO. We need to improve Qt if that API does not help, so that is why I would like to know it. :) – László Papp Mar 13 '14 at 12:57
  • I replaced the SetDllDirectory() call with `QCoreApplication::addLibraryPath(QDir::currentPath().append("/lib"));`. It didn't work. Also tried writing the path by hand with `QCoreApplication::addLibraryPath("C:/Project/lib");`, same result. – liewl Mar 13 '14 at 13:01
  • OK, if it does not work with hard coded absolute path (including a backslash trial for the path separator, too), right after initialization the application, then it would make a good feature request, at least in QtWinExtras as the last resort. Btw, make sure you have a qcoreapplication instance. – László Papp Mar 13 '14 at 13:10
  • I have a QGuiApplication instance. I tried `app.addLibraryPath()` using both QDir and the hardcoded absolute path, including using backslashes (actually double backslashes, cause it was trying to escape characters). Same results, couldn't load the library. – liewl Mar 13 '14 at 13:15
  • hmm, that is a pity, but thanks for trying! – László Papp Mar 13 '14 at 13:20

2 Answers2

2

Try using SetDllDirectory, see http://msdn.microsoft.com/en-us/library/ms686203%28VS.85%29.aspx

DmitryARN
  • 648
  • 3
  • 10
0

You should add into your system environment the path to C:/Project/lib, or from the Projects tab into your QT Creator edit the Path variable(add the path to your libraries)

Ispas Claudiu
  • 1,890
  • 2
  • 28
  • 54