2

i'm working on a C++ project with Visual Studio 2017.

I have SDL2 installed in a custom directory (not in PATH).

I'm creating a DLL which uses SDL2.

I added the SDL2 directory in the project properties (VC++ Directories -> Library Directories, I also tried with Reference Directories) and it compile correctly.

Then in the Core program, I load my DLL with LoadLibraryA and it fail (getLastError() tell me error 126).

I looked with Dependency Walker and it cannot find SDL2.DLL.

I also looked with Process Monitor, I saw it first looked for SDL2.dll in the current directory, then all directories in the PATH environment variable. Since SDL2 is not in a PATH directory, the loading fail, end of the story, me is sad.

Of course I could add the SDL2 installation directory to the PATH variable, but I don't want to, because of reasons.

Is there something I should change in my configuration?

Thanks.

SOLUTION:

I found a workaround.

My Core program load lib.dll, which needs SDL2.dll.

Instead I can create another dll, like libloader.dll. In this one I use SetDllDirectory to tell where is located SDL2.dll, and then I use LoadLibraryA to load lib.dll.

Since I have a single entry point in lib.dll, I just have to wrap it in the libloader.dll entry point.

That way, I will never have to put the location of SDL2.dll in the Core.

Ludonope
  • 963
  • 9
  • 14
  • 1
    https://msdn.microsoft.com/en-us/library/windows/desktop/ms682586(v=vs.85).aspx –  Mar 19 '17 at 00:04
  • If the dll has dependency, it only search for the dll name and not for absolute? So I have to set my PATH? I'm kindda sad – Ludonope Mar 19 '17 at 00:12

2 Answers2

2

You can work around this by using SetDllDirectory; see: https://msdn.microsoft.com/en-us/library/windows/desktop/ms686203(v=vs.85).aspx

(Note that this is somewhat non-standard behavior, and you need to be careful about security implications if you do this, but just to note it.)

Nick
  • 6,808
  • 1
  • 22
  • 34
1

Once I did something like this with SetDllDirectory:

try{
    /////////////////set dependencies subfolder relative path:
    SetDllDirectory((LPCWSTR)L".\\FDD_DLL_Dependencies\\");
    /////////////////load FDD DLL:
    HINSTANCE hDLL_FDD_DLL=NULL;
    hDLL_FDD_DLL=LoadLibrary((LPCWSTR)L"FDD_DLL.dll");
    if(hDLL_FDD_DLL==NULL){
        throw "FDD DLL did not load";
    }
}
catch(char*char_Ptr_Exception){
    std::cerr<<"Error: "<<char_Ptr_Exception<<'\n';
}
Megidd
  • 7,089
  • 6
  • 65
  • 142
  • Well in this case it's pretty much the same than with the PATH, I have to tell "directly" the Core where he can find SDL2.dll – Ludonope Mar 20 '17 at 00:14
  • 1
    @Ludonope Right, compiler doesn't know where the dependency DLL is, unless somehow you specify. – Megidd Mar 20 '17 at 02:33
  • @Ludonope I'm not sure why it is not possible for you, maybe your project has specific requirements. – Megidd Mar 21 '17 at 06:39
  • I have my Core program, and then a lib.dll which uses sdl2.dll. All I want is when I LoadLibraryA on lib.dll, lib.dll tell where to find sdl2.dll. And not putting the SDL2.dll folder in PATH, or in the Core program. Maybe there is an option in VS17, but I can't find it. – Ludonope Mar 21 '17 at 06:44
  • @Ludonope [This question](http://stackoverflow.com/q/24999520/4838962) and its corresponding [solution](http://stackoverflow.com/a/25001609/4838962) might help you. – Megidd Mar 21 '17 at 06:50
  • Oh yeah, I can do 2 dll instead of one, and the first loaded just load the second one, while setting the DllDirectory, nice ! – Ludonope Mar 21 '17 at 06:57