PDA

View Full Version : Problems using a DLL



prosass
4th March 2007, 19:19
Hello everyone. I´m using Qt 4.2.2 and VisualStudio 2005.
My problem is I wanna make use of a Dll and I only get linking errors related to it.
I want to load the dll at compiling time, so I guess I don´t need to use QLibrary, am I right?
I have a header file with the name of the functions I can call.

Here it is the .h file (loc.h):




#ifndef LOC_H
#define LOC_H

#ifdef __cplusplus
extern "C" {
#endif

#include <windows.h>
#define LOC_ERR_FRAME_INCOMPL -1
#define LOC_ERR_LONG -2
#define LOC_ERR_CRC -3

typedef struct loc_position_t {
float x;
float y;
} loc_position_t;


int loc_getposition(loc_position_t *p);
int loc_ini(void);
int loc_end(void);

#ifdef __cplusplus
}
#endif

#endif //LOC_H





I also include the dll file so you can see what it´s wrong.

I have used this code in Code::blocks without a problem, but now I wanna use it with Qt/VisualStudio and I get linking problems everytime I try to compile it.

Can anyone please help me?

fullmetalcoder
4th March 2007, 20:29
I have used this code in Code::blocks without a problem, but now I wanna use it with Qt/VisualStudio and I get linking problems everytime I try to compile it.

There is some __declspec() stuff missing here... :rolleyes: you should create a macro (LOC_EXPORT for instance) which would be defined as Q_DECL_EXPORT when building the library and Q_DECL_IMPORT when linking to it. Then it will be necessary to modify your source :


LOC_EXPORT int loc_getposition(loc_position_t *p);
LOC_EXPORT int loc_ini(void);
LOC_EXPORT int loc_end(void);


Side note : this is a Window$ specific problem encountered by every newbie that tries to create a .dll under this crappy OS without reading the docs first.:p I say that after personal experience;)

prosass
5th March 2007, 09:12
Thanks fullmetalcoder for your answer. The problem is I didn´t create the Dll and I cannot modify it to include the Q_DECL_EXPORT and the Q_DECL_IMPORT. Is there any way I can still use the DLL?

fullmetalcoder
5th March 2007, 15:37
Thanks fullmetalcoder for your answer. The problem is I didn´t create the Dll and I cannot modify it to include the Q_DECL_EXPORT and the Q_DECL_IMPORT. Is there any way I can still use the DLL?
Sounds weird... You mean that you grabbed a dll from some piece of software and want to use it? In this case you will need a SDK (header files and, under Window$, import library) and Qlibrary won't help you if the symbols (functions/classes) you are interested in are not exported using __declspec() (or any equivalent macro)...

prosass
5th March 2007, 16:25
I got the DLL from a student at my university who was working on the same piece of hardware and all I got was the file .h with the functions I can use and the Dll itself.
At the moment I don´t think I´m using QLibrary. In the .cpp file I just try to access the functions by calling them:




int err = 0;
err = loc_ini();

while (1) {
//Sleep(1);
err = loc_getposition(&loc_pos);
//Sleep(500);
cout << "Posicion x:" << loc_pos.x << endl;
cout << "posicion y:" << loc_pos.y << endl << endl;
}
}



This was enough in Code::blocks but it doesn´t work in QT/VisualStudio. Should I use something like:



QLibrary Sniffer("Sniffer");

typedef void (*MyPrototype)();
MyPrototype myFunction = (MyPrototype) myLib.resolve("mysymbol");
if (myFunction)
myFunction();



Should I use something like that?

fullmetalcoder
5th March 2007, 18:09
I got the DLL from a student at my university who was working on the same piece of hardware and all I got was the file .h with the functions I can use and the Dll itself. This was enough in Code::blocks but it doesn´t work in QT/VisualStudio.

Which files exactly do you received??? I'm particularly interested in extension like .a or .lib
AFAIK if the compile-time linking doesn't work there are not much chances for it to work at run-time but I might be proved wrong here...

prosass
5th March 2007, 22:12
I got the loc.h file that I wrote down in the beggining, the sniffer.dll and the following main.cpp:




#include "loc.h"
#include <windows.h>
#include "iostream.h"

loc_position_t loc_pos;

int main(void) {

int err = 0;
err = loc_ini();

while (1) {
//Sleep(1);
err = loc_getposition(&loc_pos);
//Sleep(500);
cout << "Posicion x:" << loc_pos.x << endl;
cout << "posicion y:" << loc_pos.y << endl << endl;
}
}



That is all I got, do I also need a .lib file to make it work?

fullmetalcoder
6th March 2007, 17:45
I got the loc.h file that I wrote down in the beggining, the sniffer.dll and the following main.cpp:
That is all I got, do I also need a .lib file to make it work?
Well, AFAIK window always require an import library to achieve linking against a dll. The file extension of these libs is .lib for MSVC and .a for gcc/mingw. If your friend did not give you such a lib you should ask for one or for the full sources...