Link with DLL from different directory

Started by
5 comments, last by android_808 15 years, 9 months ago
Hi, I've learnt how to link with DLL's using #pragma comment(lib, "filename"), however whenever I do this with a DLL I've made my program requires the DLL to be in the same directory as it. Is there a way to load a DLL in this way but from a different directory?
Advertisement
meow
The DLL lookup routines in Windows are fixed and you cannot change them.

What you can do though is add a "post build step" in your IDE of choice that copies the desired DLL into one of the DLL lookup paths.

Also for your information, check this out for precise details on the DLL lookup paths. The first directory searched is always the same directory as the application.
Simple answer: No, it's up to the OS.
Long answer: You could do what fpsgamer suggested, but that just moves the DLLs into the same directory as the EXE after compiling, so it won't help if you want to do this on a user's PC. Alternatively, you can use LoadLibrary() and GetProcAddress() to load the DLL at runtime, although that can be a pain if you have a lot of functions exported.
K then, thanks for the help.
point of reference; bumping your post after less than an hour is frowned upon.

While the forums are high traffic it isn't unheard of to wait a few hours or even a day for a reply; bumping that soon is basically rude, don't do it.
The only way I've ever done it was to create a shortcut to the application and specify a different working directory.

You could also create a small "Launcher" style application, doesn't need to even have a GUI, which changes the working directory and then executes the application.

It's taken a while but digging through the archives of my project (it's been re-written countless times, the joys of an over ambitious first project) I've uncovered the version I used.

Launcher (Seperate project, 1 file):

int main()
{
std::cout << "Launching Client..." << std::endl;
chdir("runtime");
#ifdef DEBUG
int exitCode = system("..\\Engine_D.exe");
#else
int exitCode = system("..\\Engine.exe");
#endif
return exitCode;
}

Executable is in same directory as that for main project and runtime dlls (SDL and fMod) are stored in Runtime folder. Following may help visualize.

[Project Folder]
[Data]
->Splash.jpg
[Runtime]
->SDL.dll
->FMOD.dll
Launcher.exe
Engine.exe

Only issues I come across using this is are:
* All paths need to be adjusted to cater for the fact your running from within the runtime directory. Could always have a chdir in the executable to get rid of it.
* Launcher needs to be executed with [Project Folder] as working directory or it won't find main executable. This is a simple example so there are obviously solutions, like getting install path from registry and using that or just hard coding it.

Edit: Seems to have trimmed white space. Tried to fix it, it not Data, Runtime etc are IN Project Folder

This topic is closed to new replies.

Advertisement