PDA

View Full Version : How to load DLL(C/C++) in Qt on Windows?



josecarlosmissias
20th November 2009, 14:06
Hello, everyone.

I need to load a library dynamically (the CapturaECF.dll this in a different directory): "D:\Sweda\lib"

The result is that the library can not be loaded, the message is always shown.

my project file:

--------------------------------------------------
QT -= gui

TARGET = capture
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp

LIBS += d:/sweda/lib/CapturaECF.lib
--------------------------------------------------

CPP File:

--------------------------------------------------

#include <QtCore/QCoreApplication>
#include <iostream>
#include <QLibrary>
using namespace std;

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

typedef int (*OpenDllPorta)(int porta,int veloc, QString str);

QLibrary library("CapturaECF");
library.load();

bool okLoad = library.load(); // check load DLL file successful or not
bool ok = library.isLoaded(); // check is DLL file loaded or not

if (ok) {
cout << "ok" << endl;
} else {
cout << "not ok" << endl;
cout << library.errorString().toStdString() << " " << library.fileName().toStdString() << endl;
}


return a.exec();
}

--------------------------------------------------

Coises
20th November 2009, 15:52
I need to load a library dynamically (the CapturaECF.dll this in a different directory): "D:\Sweda\lib"
[...]
LIBS += d:/sweda/lib/CapturaECF.lib
[...]
QLibrary library("CapturaECF");
library.load();


I think there are two problems here. The obvious one you can solve by reading the documentation for QLibrary::fileName-prop — you need to specify the absolute path to the library as part of the name:


QLibrary library("d:/sweda/lib/CapturaECF");

if the library is not in one of the locations the operating system normally searches for dynamically-loaded libraries.

However, I believe there is a mistake in using both LIBS (http://doc.trolltech.com/4.1/qmake-variable-reference.html#libs) and QLibrary. LIBS= links libraries (either static libraries or stubs for dynamic libraries) into the executable, so that your code calls the functions normally and any dynamic libraries are loaded automatically during start-up; QLibrary is normally be used when you don’t always want to load the library, or you don’t want the program to fail entirely if the library can’t be loaded. I could be missing something, but I think you probably want to pick one method or the other.

If you’re getting a Windows error box saying that the application could not be started because CapturaECF.dll could not be loaded, the LIBS entry is the reason. Switching to using only the QLibrary method (with the absolute path included) should fix that; but if you want to use the LIBS method, there are a few alternatives:

1. Put a copy of the DLL in the program directory.

2. Set the working directory to the directory containing the DLL. (Use the Projects / Run Settings tab in Qt Creator; set the shortcut properties to do this when running directly from Windows.)

3. Add the directory containing the DLL to the PATH environment variable.

4. Use the Windows registry; see this description of how to use HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths (http://www.codeguru.com/Cpp/W-P/dll/article.php/c99).