Unable to load DLL when process not started in the current directory (.NET Core 2.1.4)

0

This is a .NET Core 2.0 console app that loads a C++ dll (DllImport). It "works on my machine" (windows 10, .NET Core 2.1.103) in all situations. But when deployed in the staging env. it does not work if the exe is not executed from its current directory: if I run the executable from the parent directory, I get a System.DllNotFoundException: Unable to load DLL '***' : The specified module could not be found. (Exception from HRESULT: 0x8007007E)

The problem is that this process is started this way and I have no other choices! (i.e. started from a parent process located in a parent folder)

I have tried to add a SetCurrentDirectory + SetDllDirectory in the Main method w/o any effects.

To summarize:

  • If the app is started from the CLI using dotnet ***.dll, it works from any directory
  • If the app is started the same way from another process using Process.Start(), it fails
  • If the app is published as an executable, it works using the CLI from its current directory
  • But it fails from the parent directory and of course, also using Process.Start() (which is the final use case)

Last detail: the DllImport is done in the context of an Activator.CreateInstance (using Windsor)

A bit upset...

c#
.net-core
asp.net-core-2.0
dllimport
asked on Stack Overflow Mar 28, 2018 by Xavier

1 Answer

0

As Thomas suggested, it was related to the c++ runtime: it works in all situations if dependencies are all present in the current executable directory. A bit confusing as it worked in some cases without copying these dependencies (maybe a problem with the runtime installation)

answered on Stack Overflow Apr 3, 2018 by Xavier

User contributions licensed under CC BY-SA 3.0