Friday, February 11, 2011

How solve the Problem of loading a Native DLL when using ASP.NET

I have had the problem of getting ASP.NET to use a Native DLL for years. I have even posted my complaints in a previous post. My quick and dirty solution has been to simply add the path that contains my DLL to the PATH environment variable and the process would find the .dlls that way. However, if you are using a hosted ASP.NET site or have clients that simply will NOT modify the PATH environment variable, the work around does not work. I have finally found the holy grail of information on how to solve this problem. Why I have not found it before I do not know, and I have search with Google for it a hundred times.

Here is the post I found:

http://blogs.msdn.com/b/jorman/archive/2007/08/31/loading-c-assemblies-in-asp-net.aspx


For ASP.NET, the solution is very simple and elegant. I copy it directly from Jerry Orman's excellent post:

In web.config, add the following…use a path where the Native C++ DLL is located:
<appsettings><add key="NativePath" value="C:\MyNativeDLLs"> </add>

In global.asax, add the following:

protected void Application_Start(object sender, EventArgs e){
String _path = String.Concat(System.Environment.GetEnvironmentVariable("PATH"), ";", ConfigurationSettings.AppSettings["NativePath"]);
System.Environment.SetEnvironmentVariable("PATH", _path, EnvironmentVariableTarget.Process);
}


This post also details other ways of solving the problem as well.

1 comment:

Timmy said...

Great information, I’ve gained. Thank for your info