"Unable to load DLL" when C#(UWP) call native c++ dll which calls another dll


I have a UWP c# app which calls a native C++ DLL named DLL2.dll and DLL2.dll calls another native C++ DLL named DLL1.dll. I put these two dll into a folder named library and the UWP project included this folder, selecting "copy always" in "Copy to output directory" and "content" in "Build action". In .cs file:(Dll2Init will call Dll1Init)

    [DllImport("library/Dll1.dll", EntryPoint = "DLL1Init", CharSet = CharSet.Unicode)]
    public static extern void DLL1Init();
    [DllImport("library/Dll2.dll", EntryPoint = "DLL2Init", CharSet = CharSet.Unicode)]
    public static extern void DLL2Init();//this function will call DLL1Init()

    public MainPage()
        //DLL1Init();//will run well call DLL1Init before Dll2Init

When I run c#, it says "Unable to load DLL 'library/Dll2.dll' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E)". I have checked that Dll1.dll and Dll2.dll are already copied into App\bin\x64\Debug\AppX\library. And I do some tests:

  1. I add Dll1Init() before DLL2Init(), it works well. These two functions work normally.
  2. Don't add Dll1Init() before Dll2Init(). Copy Dll1.dll and Dll2.dll into App\bin\x64\Debug\AppX, and it works well too. It seems that it load dll from the same diretory(App\bin\x64\Debug\AppX) with app instead of the library.
  3. I call Dll1Init() and it works well without DLL1.dll in App\bin\x64\Debug\AppX, meaning that it loads Dll1.dll from App\bin\x64\Debug\AppX\library.

From above, it seems that when app calls dll which doesn't call other third-party dll.The dll could be in any directory in case that it DllImport it. But if the c++ dll call another Dll, these DLLs only be put into the same directory with app. It's really strange. Do I miss something? The sample is at here

asked on Stack Overflow Jul 23, 2020 by vincent

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0