Loading a DLL from the path specified by SetdllDirectory in C #

I am new to dotnet. I have a dotnet dll that loads a dll c using DllImport . I want to put all the DLLs in a folder that is different from the location of the application. I do not want to change environment variables. So I used the setdlldirectory API and loaded the C # assembly through Assembly.Loadfrom(..) . I checked that setdlldirectory working fine by checking the value of GetDllDirectory(..) . But this is not loading C # dll and c dll from this folder. I can load the C# dll by specifying the path in Assembly.Loadfrom . But failed to load c dll.

Thanks in advance!

+4
c c # windows winapi
Jul 10 2018-12-12T00:
source share
2 answers

I would suggest adding the directory path to the env PATH variable at runtime using the following code:

 var dllDirectory = @"C:/some/path"; Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH") + ";" + dllDirectory); 

Thus, the change only affects the current process and is discarded when it exits.

+13
Jul 10 2018-12-12T00:
source share

Take a look at the documentation for LoadFrom and you will find that it says: if the collector has its own image, it is not used. The assembly cannot be loaded as neutral for the domain.

I suppose you will need to add the dll to the exe path.

0
Jul 10 2018-12-12T00:
source share



All Articles