Jump to content
  • Advertisement
Sign in to follow this  
i_is_want_smart

Link with DLL from different directory

This topic is 3801 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!