How do I use functions from a DLL? I’m a total newbie and I don’t really understand how to use functions from a DLL file. I’m trying to use MS Visual Studio 2008 (C++).
My understanding is that the DLL files will have corresponding header files and as long as I include the header files and call the functions normally in my code, it should work? Is that correct? Then I would just need to have the compiled exe file be able to find the DLL?
Please let me know if that is a remotely correct understanding!
Thanks!
Russel
To reuse a function declared in a DLL you have 2 choices:
The first one (and preferable) is to include the corresponding header file declaring the function you want to use, and then linking to the corresponding .lib. This second step appears to be linking statically to the function, but in reality ends up being a stub call that will load the DLL to memory when the first function included in the corresponding DLL is called. For example, to use the CreateWindowEx function you include the “WinUser.h” header and link to the “User32.lib” library.
The second option is to load the library manually. For this you would call the LoadLibrary function to get a handle to the DLL exporing the function you want, and then use GetProcAddress to get a pointer to the function. The returned pointer needs to be cast to the appropriate type, and then you then you can use it as any regular function pointer. This option is only recommended if you do not have access to the implementer’s header and library, because there is a risk of using incorrect parameters or a mismatched calling convention in your function declaration.
PS – I’m simplifying a bit, but this is the core of how the process works.