Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a DLL with .h header in C++

I have been given a small library, consisting of a .dll, a .h header and a .def file. I'm fairly sure the library was written in C, but possibly C++.

Is it possible for me to access the functions in the library without using the LoadLibrary/GetProcAddress/FreeLibrary method that is usually talked about. I have no .lib file - is it usual to have one?

I've literally spent the past 2 days looking this up. It seems that since I've been given a header file defining the functions I wish to use from the dll, and a .def file I shouldn't need to explicitly 'load' each function manually (LoadLibrary/GetProAddress/FreeLibrary) - in my case I will be using around 5 or 6 functions from the .dll, but there are around 70 available and it seems that would be a ball-ache and result in an unnecessary mess of code.

Thanks for any advice.

like image 462
Richter Avatar asked Mar 14 '09 18:03

Richter


People also ask

How do .h files work in C?

A header file is a file with extension . h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.

Does DLL include header files?

There's no such a thing like "include header files in a dll file". What you can do is: include header files in a source code file (i.e. *.

Can you include header files in header files C?

Note: We can't include the same header file twice in any program. Create your own Header File: Instead of writing a large and complex code, we can create your own header files and include them in our program to use it whenever we want.

Can I use DLL without the header?

If you DO have appropriate . LIB file, and you have exact function prototype, you don't need header. Just declare the functions youself (possibly in your own custom header). Call those functions directly.


2 Answers

Visual C++ has "lib" - look it up in the online help.

Use "lib /def" to make the .lib file.

like image 163
Jimmy J Avatar answered Sep 28 '22 06:09

Jimmy J


You need a lib file - it contains the stubs that the linker needs to create the import table for your DLL.

Not sure if this would work, but you can try:

Create an .C file with empty stubs and the .DEF file. Compile as a DLL with the exact same name to generate the .lib. Throw away the new DLL, and link with the lib, if the existing DLL is in the same directory as your exe your program should be able to load and bind it.

I believe this will work because native DLL's are not strongly named.

like image 41
Michael Avatar answered Sep 28 '22 04:09

Michael