Extending the DLL Search Path on Windows

Ro 2005-11-29

When you load a DLL that itself loads other DLL's, Windows looks in a few places. It looks in the same directory as the executable, in the system directory, and in directories specified in the PATH environment variable.

If you don't want to change the way the system works by copying your dependent DLL's to directories that your program doesn't 'own' or by changing the PATH environment variable, you can use a simple win32 system call to extend the DLL search path.

Why would you need this? Take the example of Img 1.2.4 -- you load up img124.dll but it itself will load up a jpeg, tiff and png DLL. If you don't want to put these DLLs in the same directory as your executable, for example in a temp directory if your executable is a standalone exe that unwraps itself when run, then you can extend the DLL search path.

This uses the great ffidl extension.

  ffidl::callout dll_SetDllDirectory {pointer-utf8} int [ffidl::symbol kernel32.dll SetDllDirectoryA]

  set imgdir [file join $dir lib Img124]

  dll_SetDllDirectory $imgdir



Category Windows