Results 1 to 8 of 8

Thread: Problems using a DLL

  1. #1
    Join Date
    Feb 2007
    Posts
    15
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Problems using a DLL

    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):

    Qt Code:
    1. #ifndef LOC_H
    2. #define LOC_H
    3.  
    4. #ifdef __cplusplus
    5. extern "C" {
    6. #endif
    7.  
    8. #include <windows.h>
    9. #define LOC_ERR_FRAME_INCOMPL -1
    10. #define LOC_ERR_LONG -2
    11. #define LOC_ERR_CRC -3
    12.  
    13. typedef struct loc_position_t {
    14. float x;
    15. float y;
    16. } loc_position_t;
    17.  
    18.  
    19. int loc_getposition(loc_position_t *p);
    20. int loc_ini(void);
    21. int loc_end(void);
    22.  
    23. #ifdef __cplusplus
    24. }
    25. #endif
    26.  
    27. #endif //LOC_H
    To copy to clipboard, switch view to plain text mode 


    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?
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems using a DLL

    Quote Originally Posted by prosass View Post
    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... 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 :
    Qt Code:
    1. LOC_EXPORT int loc_getposition(loc_position_t *p);
    2. LOC_EXPORT int loc_ini(void);
    3. LOC_EXPORT int loc_end(void);
    To copy to clipboard, switch view to plain text mode 

    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. I say that after personal experience
    Current Qt projects : QCodeEdit, RotiDeCode

  3. The following user says thank you to fullmetalcoder for this useful post:

    prosass (5th March 2007)

  4. #3
    Join Date
    Feb 2007
    Posts
    15
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Angry Re: Problems using a DLL

    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?

  5. #4
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems using a DLL

    Quote Originally Posted by prosass View Post
    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)...
    Current Qt projects : QCodeEdit, RotiDeCode

  6. #5
    Join Date
    Feb 2007
    Posts
    15
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems using a DLL

    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:

    Qt Code:
    1. int err = 0;
    2. err = loc_ini();
    3.  
    4. while (1) {
    5. //Sleep(1);
    6. err = loc_getposition(&loc_pos);
    7. //Sleep(500);
    8. cout << "Posicion x:" << loc_pos.x << endl;
    9. cout << "posicion y:" << loc_pos.y << endl << endl;
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. QLibrary Sniffer("Sniffer");
    2.  
    3. typedef void (*MyPrototype)();
    4. MyPrototype myFunction = (MyPrototype) myLib.resolve("mysymbol");
    5. if (myFunction)
    6. myFunction();
    To copy to clipboard, switch view to plain text mode 

    Should I use something like that?

  7. #6
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems using a DLL

    Quote Originally Posted by prosass View Post
    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...
    Current Qt projects : QCodeEdit, RotiDeCode

  8. #7
    Join Date
    Feb 2007
    Posts
    15
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems using a DLL

    I got the loc.h file that I wrote down in the beggining, the sniffer.dll and the following main.cpp:

    Qt Code:
    1. #include "loc.h"
    2. #include <windows.h>
    3. #include "iostream.h"
    4.  
    5. loc_position_t loc_pos;
    6.  
    7. int main(void) {
    8.  
    9. int err = 0;
    10. err = loc_ini();
    11.  
    12. while (1) {
    13. //Sleep(1);
    14. err = loc_getposition(&loc_pos);
    15. //Sleep(500);
    16. cout << "Posicion x:" << loc_pos.x << endl;
    17. cout << "posicion y:" << loc_pos.y << endl << endl;
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

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

  9. #8
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems using a DLL

    Quote Originally Posted by prosass View Post
    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...
    Current Qt projects : QCodeEdit, RotiDeCode

Similar Threads

  1. Problems with Unicode(UTF8)
    By cristiano in forum Qt Programming
    Replies: 15
    Last Post: 5th December 2006, 13:33
  2. QGLWidget Problems
    By ToddAtWSU in forum Qt Programming
    Replies: 1
    Last Post: 2nd October 2006, 23:06
  3. Problems building mysql plugin for Qt 4.1.2 on windows XP
    By Philip_Anselmo in forum Installation and Deployment
    Replies: 3
    Last Post: 17th May 2006, 16:38
  4. Canvas problems
    By Tommytrojan in forum Qt Programming
    Replies: 22
    Last Post: 9th May 2006, 17:46
  5. QT4 Plugins - problems, problems
    By NormanDunbar in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2006, 16:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.