This topic is now archived and is closed to further replies.


Loading DLLs Dinamicly

Recommended Posts

Prozak    898
How does one go about loading DLLs dinamicly? I''m designing a 3D engine which supports either OpenGL or Direct3D at any one given time, but I don''t want the overhead of having Direct3D dlls loaded, if I''m using OpenGL instead... I''m searching for something like this: if( TargetAPI == OpenGL ) { dllLoad("opengl32.dll2); }; Also, do I still need to have the libraries defined in the Projects setting area after having dinamic dll loading? thanx for any input you might have...

[Hugo Ferreira][Positronic Dreams][]
"Research is what I''m doing when I don''t know what I''m doing."
- Wernher Von Braun (1912-1977)

Share this post

Link to post
Share on other sites
jenova    122
HMODULE LoadLibrary(LPCTSTR lpFileName);

and then you would need to load each function dynamically with "GetProcAddress".

i do not suggest this approach for DirectX.


To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post

Link to post
Share on other sites
Krunk    122
Just to give a bit more detail on GetProcAddress, you need to do this:

IDirect3D8 * WINAPI(*Direct3DCreate8)(UINT)
= (IDirect3D8 *WINAPI(*)(UINT))GetProcAddress(library, "Direct3DCreate8");

For every function you use from the DLL. You need to check the pointer for NULL afterwards. You might also want to use a typedef to improve readability. You don't need to include the d3d8.lib file in your project anymore.

In response to Jenova's comment, I would say one of the few times you want to use dynamic loading is with DirectX. All you need to do is get the address of the Direct-whatever-Create function. Once you`ve called that you have a fully fledged COM object and you never need to GetProcAddress again.

This is the worst thing about it though. I had a horrible bug in my program because I missed the WINAPI calling convention off the function pointer. The program worked fine in debug mode, but when I switched to release mode it went crazy. Luckily I know asm, so I worked through the disassembly and found that there was an unbalanced stack access. This meant that when I returned from the function it actually went two places up the call stack. That caused the back half of my init function to be missed out and it all went downhill from there...

Not that I`m trying to put you off

[EDIT] - added a bit about COM and dynamic loading

Edited by - Krunk on January 16, 2002 10:12:01 AM

Share this post

Link to post
Share on other sites