What is this process called? Linking a program to a DLL for more source code? Where would I learn this? Can anyone explain a bit? Thanks.
What is this process called? Linking a program to a DLL for more source code? Where would I learn this? Can anyone explain a bit? Thanks.
Last edited by Pro; 02-08-2008 at 10:11 PM. Reason: Misspelled Title
Nice name.
A DLL is a Windows-specific dynamically-loaded library. Basically, you compile your program with some statically-linked library (say, libSDL.a or SDL.lib), and then when you run your program, it looks for say SDL.dll and loads that into memory. This means several things:
- If the library is already loaded into memory, it does not have to be loaded again. This is great for really common libraries.
- If the DLL version is not what your program is expecting, you might have trouble.
- And of course, anyone who runs your program must have the DLL.
Anyway, with that out of the way . . . when you create a DLL, you basically combine a bunch of .o (or .obj) files. When you compile a .c or .cpp file, that is what you get. A DLL is just ordinary C or C++ code written in such a way that the code does not have a main(); rather, it has functions and classes that can be used in another program.
[edit] At least, that's what I think . . . I've never really used DLLs, being a Linux person. For more information, you might try google: http://www.google.ca/search?hl=en&q=...e+Search&meta= [/edit]
Last edited by dwks; 02-08-2008 at 10:43 PM.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Linking a DLL usually requires 3 things:
1) The DLL file itself.
2) A .lib file to link with, specific to the DLL. Generated when creating the DLL file.
3) A header with the prototypes of the functions exported by the DLL.
What you do is include the header file, the add the .lib file under "additional libraries" or such under your linker so it searches the lib file.
Then you can call all those functions in the dll as if they were in your own program. This process is called static linking.
There is another way called dynamic linking.
In this one, you don't need a .lib file, nor a header really, but you still need to know what functions are exported and how to call them correctly, so a header is a good thing nevertheless.
Anyway, you first load the dll using LoadLibrary.
You then find the function you want to call using GetProcAddress, whose return you should cast to a function pointer. Then you can call that function pointer to call the function of choice. Then you close the library using FreeLibrary.
Static linking requires the DLL to be present with your application, otherwise Windows will complain "X.dll couldn't be found. Try reinstalling the application." Your program will never even begin to run.
Dynamic linking has the advantage that you can check if the dll exists and print errors or whatever, but your program can run without the dll.
Code:// Let's find the function "foo", whose prototype is "void foo(int n)". typedef void (FooFncPtr)(int); HMODULE hDll = LoadLibrary("my dll.dll"); FooFncPtr* pFoo = (FooFncPtr*)GetProcAddress(hDll, "foo"); pFoo(); pFoo = NULL; FreeLibrary(hDll);
There is no such thing as statically linking to a DLL. DLL stands for Dynamic Link Library, therefore all DLL linking is dynamic. You are getting static linking mixed up with implicit linking.Static linking requires the DLL to be present with your application, otherwise Windows will complain "X.dll couldn't be found. Try reinstalling the application." Your program will never even begin to run.
Thats called explicit linking to a DLL file. The only header file you need to do that is windows.h.Thanks once again! What header file would you use to do this? Is this static or dynamic linking? Thanks.
What are the big difference between implicit, dynamic, and static linking? Thanks lol.
It's no problem. One of you spelled it explicit linking, and the other implicit linking. Which one is the right one?, lol.
So one uses a .lib file and the other doesn't? That is the main difference right? Just checkin'..