Kasper
Kasper

Reputation: 690

LoadLibraryA and relative path

I'm trying to load a dll (through LoadLibraryA) from another dll. This is the problem:

c:\**EXE_DIR**\myExe.exe             // this exe load the MY_DLL_N1.dll
c:\**DLLS_DIR**\MY_DLL_N1.dll    // this dll load the MY_DLL_N2.dll
c:\**DLLS_DIR**\MY_DLL_N2.dll
  1. the exe load the MY_DLL_N1.dll ... fine.
  2. MY_DLL_N1.dll try to load (below the code) the MY_DLL_N2.dll (same dir) ... and here is my problem!

int LoadMyDLL()
{
    // ...

    // same path of the MY_DLL_N1.dll ... right?
    handle = LoadLibraryA ("MY_DLL_N2.dll");

    // ...
}

that's all .... any help is welcome!

Upvotes: 1

Views: 11054

Answers (2)

selbie
selbie

Reputation: 104569

Everything you need to know is located here: Dynamic-Link Library Search Order.

Consider using SetDllDirectory to add your DLL path to the LoadLibrary search path.

Upvotes: 4

David Heffernan
David Heffernan

Reputation: 613442

handle = LoadLibraryA ("MY_DLL_N2.dll");

Because you do not supply a path, the DLL search order is used. This will look in the executable's directory, but will not search in the directories of any DLLS that are loaded. Hence the failure to find the DLL.

You have a number of options:

  1. Put all the DLLS in the same directory as the executable.
  2. Pass the full path to the DLL when calling LoadLibrary.
  3. Call SetDllDirectory to add the DLL directory to the search path. Make this call from the executable before loading the first DLL. Once you do this you won't need to use the full path when loading the first DLL.

Unless you have a need to share the DLLS between different applications, option 1 is always preferred. This makes it easy for you to be sure that the DLLs you load are the right ones. That's because the executable directory is always searched first.

Upvotes: 2

Related Questions