Load a DLL from another directory at program start

25,873

Solution 1

There are two types of dynamic linking in the Windows world:

  1. Load-Time linking is when a DLL is loaded automatically when your program starts up. Windows finds this DLL using a specific algorithm I'll discuss below.
  2. Run-Time linking is when you specifically load a DLL by calling LoadLibrary in your code. Similar rules apply as to how the library is found, but you can specify a fully-qualified or relatively-qualified path to control the search.

In the case of Load-Time linking, MS recommends that your program's DLLs are stored in and loaded from the same directory where your application is loaded from. If this is at all workable, this is probably your best option.

If that doesn't work, there are several other options, outlined here. One is to leverage the search order by putting the DLL in either the working directory or the directory where the application was loaded from.

You can change the working directory of an application by:

  1. Create a shortcut to your application.
  2. Bring up the shortcut's properties
  3. Edit the "Start in" property with the directory where the DLL is located.

When you launch your application using the shortcut, it will load the right DLL.

Other options for load-time linking include:

  • Adding a manifest to your application which specifies where your dependent assemblies are, or,
  • Setting the PATH.

Solution 2

You could use LoadLibrary, but you would need a way to guarantee the DLL's location. This Wikipedia article provides good example on how to use the DLL after it has been loaded.

Solution 3

I have struggled with the same problem and also found a dead end with the suggested methods like LoadLibrary, SetDllDirectory, Qt's addLibraryPath and others. Regardless of what I tried, the problem still remained that the application checked the libraries (and didn't find them) before actually running the code, so any code solution was bound to fail.

I almost got desperate, but then discovered an extremely easy approach which might also be helpful in cases like yours: Use a batch file! (or a similar loader before the actual application)

A Windows batch file for such a purpose could look like this:

@echo off
PATH=%PATH%;<PATH_TO_YOUR_LIB>
<PATH_TO_YOUR_APP_EXE>

/edit: Just saw @SirDarius comment in Luchian's answer which describes that way, so just take my batch code bit as a reference and all credits go to him.

Solution 4

You can add the directory where the dll is located to the PATH environment variable.

Solution 5

I have the same problem with one application I am working on.

I do not want to use runtime loading because there are tens of functions I would need to manually create function pointer for.

Mr Dibling's mention of manifest file opened a new door for me but I sadly found out that the oldest version of windows that supports the feature is Windows 7. It won't even work on Vista.

Long story short, a friend familiar with Windows Application development told me to look up Delay-Loaded DLL, which turns out to solve the problem perfectly with minimal effort. It delays the loading of DLL library to either the point you manually do, or the first time its function is called. So you just need to add your DLL path to the search path before that happens, where SetDllDirectory helps.

Here is the steps to make it work:

1) Specify the DLL to be delay-loaded to linker, either through your makefile, cmake or VS property page (Linker->Input of VS2015)

2) Call SetDllDirectory at the beginning of your program, before any call to the DLL is made.

Delay-loaded DLL is supported all the way back to VC6. SetDllDirectory is supported after XP SP1.

Share:
25,873
CJ McAllister
Author by

CJ McAllister

Updated on July 12, 2022

Comments

  • CJ McAllister
    CJ McAllister almost 2 years

    My basic issue is this: my program (MyProgram.exe) has a dependency on a DLL from another program (OtherProgram), and I'm trying to avoid repackaging a new DLL every time OtherProgram updates. I'd like to have MyProgram.exe link in OtherProgram's DLL when it launches, but I'm not completely sure that Windows allows for this. So if there is some kind of workaround that would also be acceptable.

    And just for some background, the platform is Windows 7 x64, and MyProgram.exe runs fine when I create a symlink in the MyProgram.exe project directory to the DLL in OtherProgram's install directory. When I try to run it without the symlink, I get the "program can't start because OtherProgramDLL.dll is missing from your computer" error.

    Any advice or links to relevant info is greatly appreciated!

    EDIT: Clarification: the DLL is not linked at compile-time, this issue crops up at runtime

  • CJ McAllister
    CJ McAllister almost 12 years
    Worked! Can't believe I didn't think of that, haha. Thanks!
  • Luchian Grigore
    Luchian Grigore almost 12 years
    Wrong. If the exe is linked against the library, wherever you call LoadLibrary, it's too late.
  • John Dibling
    John Dibling almost 12 years
    I didn't downvote, but you do not typically want to LoadLibrary a DLL. Certianly not just so you can specify the path where it lives.
  • John Dibling
    John Dibling almost 12 years
    @LuchianGrigore: Well, to be fair, it could be un-linked from the library.
  • David Heffernan
    David Heffernan almost 12 years
    @JohnDibling I disagree with your first comment. There are various scenarios where LoadLibrary is the best choice. And your second comment is quite correct, that's clearly what this answer envisions.
  • Luchian Grigore
    Luchian Grigore almost 12 years
    Yes there are situations where LoadLibrary is actually the only option. But you shouldn't use it or un-link as you say when there are better options available.
  • David Heffernan
    David Heffernan almost 12 years
    Whilst you can do this, you are using a sledgehammer to crack a nut. Whenever I encounter a program that changes the global PATH variable, I get a bad feeling.
  • John Dibling
    John Dibling almost 12 years
    @David: Agreed. Don't screw around with the PATH. It will just screw you later.
  • David Heffernan
    David Heffernan almost 12 years
    @LuchianGrigore What are the better options then. I strongly disagree that modifying PATH is a better option than this.
  • Eckstein
    Eckstein almost 12 years
    @LuchianGrigore I don't believe his question stated his executable linked to the library at compile time, only that it has a dependency. If the DLL in "OtherProgram" changed when it updated he'd have to relink anyway.
  • John Dibling
    John Dibling almost 12 years
    @Eckstein: Only if the ABI changed. Simple bugfixes, with no changes in function signatures, would not need to be relinked for example. This is one of the big benefits of using DLLs in the first place.
  • Eckstein
    Eckstein almost 12 years
    @JohnDibling Correct, but in response to Luchian's comment, calling LoadLibrary is not too late.
  • CJ McAllister
    CJ McAllister almost 12 years
    @Eckstein I do believe Luchian is correct in that. I tried adding LoadLibrary as the first action within WinMain of MyProgram.exe, and it did not resolve the issue. However, I'm not all that familiar with COM and such, so there may be another location besides WinMain that would be more appropriate.
  • David Heffernan
    David Heffernan almost 12 years
    @cjm571 No I don't think you understand. You have to use LoadLibrary and GetProcAddress and stop using the .lib file that results in what is known as implicit linking. It certainly makes like more inconvenient if there are a lot of functions to link to, but you gain flexibility.
  • SirDarius
    SirDarius almost 12 years
    another option is to use a loader program that searches the dll's path, and runs 'MyProgram.exe' with an altered non global PATH environment variable.
  • Harry Johnston
    Harry Johnston almost 12 years
    Using the current directory to find a DLL is legacy behaviour, and won't work if the system administrator has implemented KB2264107.
  • CJ McAllister
    CJ McAllister almost 12 years
    @DavidHeffernan Ah ok, I see. Thanks for the clarification
  • xiay
    xiay over 6 years
    SetDllDirectory won't work without delay-loaded dll because the compiler would ask the program to try load the DLL before your call to SetDllDirectory is made. See my answer below.
  • Vizor
    Vizor over 5 years
    This KB only add option for admin, that he can remove cwd from dll search path, but cwd is still searched in by default.