[SDL] Dynamically loading SDL dlls

Michael Parker aldacron at gmail.com
Mon Oct 29 23:57:29 PDT 2007


On 10/30/07, Jeremy <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
   ...
}


-- 
Mike Parker
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20071030/b50c9d30/attachment.html 


More information about the SDL mailing list