PDA

View Full Version : How to use QLibrary?



digog
3rd November 2010, 03:40
Hello,
I would like to know how to use the QLibrary to read a DLL library and call its functions.
I'll appreciate if someone could give me an example.

Thanks

tbscope
3rd November 2010, 04:20
You don't use QLibrary to use a DLL in your application.

Just include the header file of the library, and link the library to your application.

Note that you can not use QLibrary to easily load C++ symbols. So, if your library is written in C++ and it doesn't extern C its symbols, you'll need a bit of work to get the correct symbols.

Robbie
3rd November 2010, 06:42
You cannot use QLibrary to scan a shared library to see what symbols it has. You need to get that list from the library's creator.

Once you know what's inside the library, you need to know the types of the symbols that you wish to access. These could be functions, classes or variables. In the case of functions, you need to know the function signature, in the case of classes, you need the class definition and in the case of variables, you need to have the data type. All these should be supplied by the creators in header files and/or DEF files.

Once you've decided what you need, you can load the library into memory using QLibrary (see Assistant) and then find the symbol's address using QLibrary::resolve(), converting the void* to the data type that the symbol represents.

digog
3rd November 2010, 15:09
how about if I use the function LoadLibrary? is that supported by QT?

Added after 11 minutes:


You cannot use QLibrary to scan a shared library to see what symbols it has. You need to get that list from the library's creator.

Once you know what's inside the library, you need to know the types of the symbols that you wish to access. These could be functions, classes or variables. In the case of functions, you need to know the function signature, in the case of classes, you need the class definition and in the case of variables, you need to have the data type. All these should be supplied by the creators in header files and/or DEF files.

Once you've decided what you need, you can load the library into memory using QLibrary (see Assistant) and then find the symbol's address using QLibrary::resolve(), converting the void* to the data type that the symbol represents.

Thanks for the reply Robbie.
I already know what is inside the library. I'm talking about the mpusbapi.dll from microchip, I already have all the headers and sources code but specific to the boorland c++ builder compiler, so from there I can get the list of the function that I want to use.
How can I proceed from here? I need to make a DEF file?

wysota
3rd November 2010, 15:25
how about if I use the function LoadLibrary? is that supported by QT?
What is it that you really want to do with the library? Why can't you link your program against it during compilation?

digog
3rd November 2010, 15:30
What is it that you really want to do with the library? Why can't you link your program against it during compilation?

I cant link during compilation cause I just have the dll file I dont know how to get the .h file from it.
I'll be glad if you could explain me how.

tbscope
3rd November 2010, 15:43
I dont know how to get the .h file from it.


I already have all the headers

I guess there's something fundamental you do not understand.

You have the DLL, that's ok.
You have the headers (.h file) that's great.

Now, include the header file in your own source code and link the dll with your program.
Something in the lines of LIBS += -lmpusbapi should be added to your .pro file

wysota
3rd November 2010, 16:38
I think what he really lacks is the import library but as far as I know this can be regenerated from the dll.

digog
3rd November 2010, 22:14
You have the DLL, that's ok.
Thats ok.


You have the headers (.h file) that's great.
The .h file is specific to the borland c++ compiler. I don't know if I can just include this .h file in my own project.


link the dll with your program.
Something in the lines of LIBS += -lmpusbapi should be added to your .pro file
dont I need to have the .lib file for this? I have one here but, as I said before, its specific to the borland c++ compiler.

Sorry if I making a big confusion, its just all new for me.

wysota
3rd November 2010, 22:23
The .h file is specific to the borland c++ compiler. I don't know if I can just include this .h file in my own project.
The .h file holds the function prototypes which you will need with QLibrary too. So this is a problem you will have to resolve either way.


dont I need to have the .lib file for this? I have one here but, as I said before, its specific to the borland c++ compiler.
http://jrfonseca.planetaclix.pt/projects/gnu-win32/software/reimp/index.html
I'm sure there is an equivalent for Turbo C++.

digog
4th November 2010, 00:01
http://jrfonseca.planetaclix.pt/proj...imp/index.html
I'm sure there is an equivalent for Turbo C++.
What should I do with it?

wysota
4th November 2010, 08:29
Reconstruct the import library from the dll.

digog
4th November 2010, 14:52
Reconstruct the import library from the dll.
Isn't there a tutorial?

And do you think that this program, that I found on internet, can resolve my problem?
Here is the link:
http://www.softpedia.com/get/Programming/Other-Programming-Files/DLL-to-Lib.shtml

wysota
4th November 2010, 14:58
Isn't there a tutorial?

Did you find the appropriate program?

digog
5th November 2010, 00:23
Did you find the appropriate program?
I downloaded the mingw-get-inst-20101030 and mingw-utils-0.3.tar.gz. Is those the right programs?

wysota
5th November 2010, 00:50
I downloaded the mingw-get-inst-20101030 and mingw-utils-0.3.tar.gz. Is those the right programs?

I don't know, is there a tool there that can create an import library for MinGW from a Borland DLL?

digog
5th November 2010, 02:07
I don't know, is there a tool there that can create an import library for MinGW from a Borland DLL?
The dll is not a Borland DLL, it is compiler independent as you can see in the readme file that comes with the dll:

Release Notes for MPUSBAPI Library, Microchip Technology Inc.
v1.00
19 November 2004

----------------------------------------------------------------------
1. What is MPUSBAPI Library?
----------------------------------------------------------------------

* MPUSBAPI is a DLL module providing wrapper functions for the
Microchip General Purpose USB Windows driver, mchpusb.sys.
It abstracts out the complexity in working with Win32 API functions.

* Seven basic functions are provided in this release:

- MPUSBGetDeviceCount

- MPUSBOpen

- MPUSBClose

- MPUSBRead

- MPUSBReadInt

- MPUSBWrite

- MPUSBGetDLLVersion

* Details on how to use each function are documented in the source code
- Refer to \DLL\Borland_C\Source\_mpusbapi.cpp

----------------------------------------------------------------------
2. Linking MPUSBAPI to your project
----------------------------------------------------------------------

There are two ways to link the DLL to your project:

a) Load-time Linking

Simply add the mpusbapi.lib file to your project.
The mpusbapi.lib file provided is specific to the Borland C++
compiler. The mpusbapi.dll is compiler independent. If a different
compiler is used, however, the DLL must be re-compiled to generate
a new mpusbapi.lib specific to that compiler.

C++ source code for MPUSBAPI is provided.

The _mpusbapi.h file must be included in every file that uses
the MPUSBAPI functions.

The mpusbapi.dll file must be in the same directory as the
executable file.

b) Run-time Linking

Run-time Linking does not require the .lib file. It only requires
the .dll file. Linking the functions is, however, a little more
complicated. An example is provided showing how to use Win32
API functions to load a DLL.

The mpusbapi.h file (note, no "_") must be included in every file
that uses the MPUSBAPI functions.

The mpusbapi.dll is not required to be in the same directory as
the executable file.
So I think that I just need a program that could create an import library compatible with MinGW, right?
What about the dlltool? can it resolve my problem?

tbscope
5th November 2010, 04:28
If a different compiler is used, however, the DLL must be re-compiled to generate a new mpusbapi.lib specific to that compiler.

As easy as that.

digog
5th November 2010, 15:07
As easy as that.
Ok, so we are close now.
Do you know any tool that can do it? In other words, create an import library compatible with MinGW?

wysota
5th November 2010, 15:27
Do you know any tool that can do it? In other words, create an import library compatible with MinGW?
Yes, it's called a "compiler". It takes the source code as input and outputs the import library and runtime library.

digog
5th November 2010, 17:02
Yes, it's called a "compiler". It takes the source code as input and outputs the import library and runtime library.

So you are saying that I can use the source code but its made in borland c++ compiler, I don't think that its compatible with MINGW. So my idea was to reconstruct the import library from the DLL, so I ask if someone knows a tool that can do it.

tbscope
5th November 2010, 17:06
Unless the code contains macro's only available to Turbo C++, you can just recompile it with any other compiler. Of course, all the other dependencies need to be met too.

At least, the documentation you copied here on the forum states it should be possible.

digog
5th November 2010, 18:01
Unless the code contains macro's only available to Turbo C++, you can just recompile it with any other compiler. Of course, all the other dependencies need to be met too.

At least, the documentation you copied here on the forum states it should be possible.

I tried to do that, but I get a lot of errors:

D:/Qt/2009.03/qt/QT_teste/mpusbapi/_mpusbapi.cpp:113: error: cannot convert `char*' to `WCHAR*' for argument `1' to `int wsprintfW(WCHAR*, const WCHAR*, ...)'
D:/Qt/2009.03/qt/QT_teste/mpusbapi/_mpusbapi.cpp:120: error: cannot convert `char*' to `const WCHAR*' for argument `2' to `LONG RegOpenKeyExW(HKEY__*, const WCHAR*, DWORD, REGSAM, HKEY__**)'
D:/Qt/2009.03/qt/QT_teste/mpusbapi/_mpusbapi.cpp:128: error: cannot convert `const char*' to `const WCHAR*' for argument `2' to `LONG RegQueryValueExW(HKEY__*, const WCHAR*, DWORD*, DWORD*, BYTE*, DWORD*)'
D:/Qt/2009.03/qt/QT_teste/mpusbapi/_mpusbapi.cpp:277: warning: passing NULL used for non-pointer converting 4 of `DWORD MPUSBGetDeviceLink(DWORD, CHAR*, CHAR*, DWORD, DWORD*)'

most of them was like these.