PDA

View Full Version : How to use dll on Windows (2)



schall_l
21st April 2008, 16:51
I have a similar request as Merry in a previous post today.

As this post was not answered I would like to be a bit more precise at least concerning my problem.

Let's say I am writing an application using QT4 that is supposed to use the driver dll from a third vendor hardware device. The device comes with a .dll file, with a .lib file and with a .h file describing the driver interface. I have no possibility to recompile the .dll and the .lib files as they come as is, with the hardware device.

For example let's take a fictive USB device implementing some leds. The driver allows the driving of it led's and comes with 3 files: ledviausb.dll, ledviausb.lib and ledviausb.h (see attachement)

My question is to know how to integrate this into my QT4 project ?

First I suppose that I would need to specify the .lib file in the project file ?

LIBS += driver/ledviausb.lib
INCLUDEPATH += driver

Than I would need to address it from my application ?


#include "ledviausb.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication app(argc,argv);
CLEDviaUSB*test = new CLEDviaUSB();
test->OpenDriver(0);
test->SwitchOnLed(0,1);
test->SwitchOnLed(0,2);
return 0;
}

Do I need to rewrite the .h file to follow some QT4 specifications, because currently the compilation would give me something like:

1>LINK : debug\dll_test_app.exe not found or not built by the last incremental link; performing full link
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: short __stdcall CLEDviaUSB::SwitchOnLed(unsigned char,short)" (__imp_?SwitchOnLed@CLEDviaUSB@@QAGFEF@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: short __thiscall CLEDviaUSB::OpenDriver(unsigned char)" (__imp_?OpenDriver@CLEDviaUSB@@QAEFE@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall CLEDviaUSB::CLEDviaUSB(void)" (__imp_??0CLEDviaUSB@@QAE@XZ) referenced in function _main
1>debug\dll_test_app.exe : fatal error LNK1120: 3 unresolved externals
1>Build log was saved at "file://c:\tmp\canmonitor\lab\dll_test_app\debug\BuildLog. htm"

wysota
21st April 2008, 17:09
Let's see... according to me you need to add the following to your project file:
LIBS+=-Ldriver -lledviausb

HEADERS+=ledviausb.h

schall_l
21st April 2008, 17:21
Well I am getting the same kind of linker errors:

I do not understand anyway what would have been the difference under windows between


LIBS += driver/ledviausb.lib
and
LIBS+=-Ldriver -lledviausb

wysota
21st April 2008, 17:29
Was the library built for the same compiler you are using to compile your code? Maybe the name mangling mechanism is different?

Tavo92
21st April 2008, 20:27
Is the header right? It may have an import error.

Why don't you try to use dinamic linking? You do not need any header as far as i know but it is more difficult to use. The dinamic linking is done using QLibrary (Check it on the Qt Assistant).