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.
Printable View
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.
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]
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.Quote:
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.
This is dynamic.
All of these functions are located somewhere in the nest of Windows.h. And you would like to have a header to the dll that shows the exported functions from the dll and the prototypes of them.
Thats called explicit linking to a DLL file. The only header file you need to do that is windows.h.Quote:
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.
No such thing as static linking as bithub mentioned. It's called implicit linking. It was my bad >_<
It's no problem. One of you spelled it explicit linking, and the other implicit linking. Which one is the right one?, lol.
Oh I see bithub just called it explicit. However, according to MSDN, it's called implicit linking.
But even so, the name doesn't really mean that much. It's the first method I mentioned, using a .lib file.
So one uses a .lib file and the other doesn't? That is the main difference right? Just checkin'..
No, the main difference is that dynamic linking allows you load an unload dlls at any time. It also allows you to dynamically specify which dlls to load, as opposed to implicit linking. Dynamic linking will also allow to ignore if a dll fails to load or provide your own custom error messages.
Where does it say that? By calling LoadLibrary() you are explicitly linking to the DLL file. By using a .lib file with no LoadLibrary() call, you are implicitly linking to the DLL file. I'd like to see where MSDN says otherwise. Especially considering they make it very clear right here.Quote:
Oh I see bithub just called it explicit. However, according to MSDN, it's called implicit linking.
But even so, the name doesn't really mean that much. It's the first method I mentioned, using a .lib file.
:S This is getting a little confusing. Oh well, would you really need to know the difference when all you're trying to do is link a DLL to a program?
http://msdn2.microsoft.com/en-us/library/d14wsce5.aspx
Implicit linking is using a .lib file. Explicit linking, aka dynamic linking, is using LoadLibrary and GetProcAddress.
You should. But most times you only need to use implicit linking (.lib file).
Dynamic linking might be useful if you need to, for example, load all dlls in a certain directory (for example, plugins).
Well, actually, the link states that there are two methods:
Implicit linking (also known as static linking or load-time linking)
and
Explicit linking (also known as dynamic linking).
It does an automatic GetProcAddress for you, to put it simply. So the function calls are made correctly.
Do the .lib files come with the compiler? Because I think I know what you're talking about.
Yes, they typically do for dll files for the C++ standard libraries (and C too).
You'll also find them in SDKs such as Windows SDK to link statically with Windows DLLs (such as winmm.dll).
No, they are sometimes called static or dynamic loading. Once again, if it is a DLL file then it is always dynamic linking.Quote:
Well, actually, the link states that there are two methods:
Implicit linking (also known as static linking or load-time linking)
and
Explicit linking (also known as dynamic linking).
Where does it say that it's always dynamic linking when it's dlls? Your link certainly doesn't mention.Quote:
Implicit linking is sometimes referred to as static load or load-time dynamic linking. Explicit linking is sometimes referred to as dynamic load or run-time dynamic linking.
It's always a dynamic link with DLL files because the link is always done at runtime. A static link is done at compile time (these are done with .a files, not .dll files). Hense the name Dynamic Link Library.Quote:
Where does it say that it's always dynamic linking when it's dlls? Your link certainly doesn't mention.
There is such method as delay-load dlls
you link dll using lib-file,
you do not need to use LoadLibrary and GetProcAddress functions
but you can check if the library is available - and if not - your application still can be loaded without showing this nice windows message "File not found" and work successfully, just not calling any function from dll
Can someone give me an example of both the DLL, and the program itself? Thank you.
Why don't you do the DLL and ask for help in making the application use it, if you can't figure it out yourself?