[SDL] Dynamically loading SDL dlls

Andre Krause post at andre-krause.net
Tue Oct 30 03:09:00 PDT 2007


Michael Parker wrote:
> 
> 
> On 10/30/07, *Jeremy* <jswigart at gmail.com <mailto:jswigart at gmail.com>> 
> wrote:
> 
>     Is there an alternative to SDL_SetLibraryPath ?
> 
>     I'd like to load the SDL libraries from a folder of my choice, one
>     that isn't the executables folder, and isn't in the path. Is there
>     no support for doing this? Some documentation implies the dll
>     shouldn't be loaded/required until you call SDL_Init, but without
>     the dll in the path, windows folder, app folder, etc. My dll that
>     uses SDL fails to load, which indicates it does indeed need the sdl
>     library well before SDL_Init. SDL_SetLibraryPath appears to be what
>     I would use in this case but it seems to have been removed some time
>     ago. 
> 
> 
> If your dll is failing to load, that tells me that you are linking with 
> the import library still. When you want to load another dll manually, 
> you can't link to the import library. Otherwise, Windows will attempt to 
> load the dll from the usual locations and fail if it can't find it.
>  
> 
>     Any alternatives?
> 
> 
> If you're going to pursue this, you'll have to use the 
> LoadLibrary/GetProcAddress routines on Windows, and libdl or whatever is 
> appropriate on other platforms. It also means that you'll have to 
> convert all of the function declarations in the SDL headers into 
> function pointer declarations and then load each of them one at a time. 
> So you'd get something like this:
> 
> // in header
> typedef int (*pfSDL_Init)(Uint32);
> 
> pfSDL_Init SDL_Init;
> 
> // in source (for Windows)
> static HMODULE s_sdlLib = 0;
> 
> void loadSDL()
> {
>     s_sdlLib = LoadLibrary("SDL.dll ");
>     if(!s_sdlLib) ...
> 
>     SDL_Init = (pfSDL_Init)GetProcAddress(s_sdlLib, "SDL_Init");
>     if(!SDL_Init) ...
> 
>     // repeat for each function in the library
>    ...
> }
> 

what Mike says is so true... and so painful.
imho this renders the dll concept inappropriate
for some special cases - like loading a dll from a
memory place (there are solutions, but they are even worse)
or from a resource archive or just some arbitrary subdirectory.

i suggest you to statically link libSDL (and as much
other libs as you can), this saves you from a lot
of trouble.

> 
> -- 
> Mike Parker
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> SDL mailing list
> SDL at lists.libsdl.org
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org



More information about the SDL mailing list