Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading dlls from path specified in SetdllDirectory in c#

Tags:

c

c#

windows

winapi

I am new in dotnet.I have a dotnet dll that loads a c dll by using DllImport. I want to place all the dlls in a folder which is diffrent from the location of application. I dont want to modify environmental variables. So i used setdlldirectory API and load my c# assembly through Assembly.Loadfrom(..). I checked that SetdllDirectory is working fine by verifying the value of GetDllDirectory(..). But it is neither loading C# dll nor c dll from that folder. I am able to load C# dll by specyfing the path in Assembly.Loadfrom. But not able to load c dll.

Thanks in advance!!

like image 656
Rohit Garg Avatar asked Jul 10 '12 10:07

Rohit Garg


1 Answers

I'd suggest adding the directory path to PATH env variable in runtime, using the following code:

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

That way, the change is only affecting the running process and is discarded when it exits.

like image 50
Tomek Avatar answered Nov 06 '22 22:11

Tomek