views:

1068

answers:

5

I understand that I need to use LoadLibrary(). But what other steps do I need to take in order to use a third party dll? I simply jumped into C++ and this is the only part that I do not get (as a Java programmer). I am just looking into how I can use a Qt Library and tesseract-ocr, yet the process makes no sense to me and is so difficult to google.

Update: How do i Tell the compiler of the functions that I am using? Should there be an include file from the third party vendor?

+4  A: 

These are two ways of using a dll in windows:

  1. There is a stub library (.lib) with associated header files. When you link your executable with the lib-file it will automatically load the dll when starting the program.

  2. Loading the DLL manually. This is typically what you want to do if you are developing a plugin system where there are many dll files implementing a common interface. Check out the documentation for LoadLibrary and GetProcAddress for more info on this.

For Qt I would suspect there are headers and a static library available that you can include and link in your project.

Laserallan
A: 

I'f you're suppsed to be able to use it, then 3rd-party library should have a *.lib file as well as a *.dll file. You simply need to add the *.lib to the list of input file in your project's 'Linker' options.

This *.lib file isn't necessarily a 'static' library (which contains code): instead a *.lib can be just a file that links your executable to the DLL.

ChrisW
+2  A: 

You only need to use LoadLibrary if you want to late bind and only resolve the imported functions at runtime. The easiest way to use a third party dll is to link against a .lib.


In reply to your edit:

Yes, the third party API should consist of a dll and/or a lib that contain the implementation and header files that declares the required types. You need to know the type definitions whichever method you use - for LoadLibrary you'll need to define function pointers, so you could just as easily write your own header file instead. Basically, you only need to use LoadLibrary if you want late binding. One valid reason for this would be if you aren't sure if the dll will be available on the target PC.

Stu Mackellar
+1  A: 

In order to use Qt with dynamic linking you have to specify the lib files (usually qtmaind.lib, QtCored4.lib and QtGuid4.lib for debug config) in Properties->Linker->Input->Additional Dependencies. You also have to specify the path where the libs are in Properties->Linker->General->Additional Library Directories.

And you need to make the corresponding .dlls accessible at runtime either by putting them in the same folder as your exe or in a folder that's on your path.

drby
+4  A: 

As everyone else says, LoadLibrary is the hard way to do it, and is hardly ever necessary.

The DLL should have come with a .lib file for linking, and one or more header files to #include into your sources. The header files will define the classes and function prototypes that you can use from the DLL. You will need this even if you use LoadLibrary.

To link with the library, you might have to add the .lib file to the project configuration under Linker/Input/Additional Dependencies.

Mark Ransom
Thanks, the other answers were very good (and useful for me as well) but this explained exactly what I was looking for.
Zombies