Reputation: 47
I recently was very confused in trying to use gdi+, since the sdk files where just three files, one of which being a .dll file. I assumed I need to load the dll and since I had never been taught or exposed to such things, needless to say I was bewildered. (it now seems that the installation also put gdi+ sdk files in the folder which VS looks for standard files (the things that are included via <>, and still leaves me clueless as to what the dll is doing there).
I researched and got an explanation that seemed to suggest the following:
A .dll file is a file who's code is loaded into memory when it is executed, saving time by referencing the memory via pointers rather than copying the code as a standard lib load does. This also allows versatility for changing the dll without changing the executable file.
To use it, you must use loadlibrary, and then getprocaddress to basically get a pointer (or something, wasn't clear to me) to specific elements, such as a class or function, in the dll.
You see, I was under the impression that a library is basically code you are to reuse for things, the same concept, but a more efficient form of simply makes .cpp files of useful methods and classes and then including them.
From what I can tell, this assumption is incorrect and I would like someone who knows the inside and out of this .lib and .dll library thing to explain it if they please.
Please remember that my current understanding of the purpose of a dll or lib is to reuse code. Make a file with classes and methods and then just import it in to use it. I am extremely inexperienced with libraries in this sense.
Upvotes: 1
Views: 9697
Reputation: 4275
A .dll and a .lib are as you said options to reuse code. A .dll is called dynamic library, because the library is loaded at runtime, while a .lib is a static library, loaded and put into your program at compile time. I'm aware of three options to use these libraries:
LoadLibrary()
to load the .dll into memory and then use GetProcAddress
to get a function pointer (basically a memory address in a variable, but you can use it just like a function). The problem here: you need to know the mangled names of the functions (mangled = compiler generated unique names for function overloads and similar) and you need to know the function-arguments and return values..lib
) which actually just contain stub code which then loads the .dll
containing the actual code at the start of your program. This will give you the best of both possibilities, since you dont have to mess with name-mangling/function pointers at all while still loading the library at runtime. This also means you only have to recompile your program once the API (=header) of your library changes.LoadLibrary
you can fail gracefully), but it does significantly reduce the programmer's burden..winmd
) essentially serves as a kind-of "export list". It can be considered a kind of "COM on Steroids" or "COM for the 21st Century" if you're feeling mean.Upvotes: 6
Reputation: 100151
To use a DLL, you:
#include
the header file when you compile..lib
file (the 'implib') in the link process, so that the linker knows that the symbols are coming at runtime from the dll.PATH
environment variable.Note that in some versions of Windows, this process is complicated by SxS considerations, and you will need a manifest. You can look up SxS and the related issues for yourself.
Upvotes: 2