Sign in to follow this  

Link with DLL from different directory

This topic is 3460 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 3460 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this