Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unmanaged DLLs fail to load on ASP.NET server

This question relates to an ASP.NET website, originally developed in VS 2005 and now in VS 2008.

This website uses two unmanaged external DLLs which are not .NET and I do not have the source code to compile them and have to use them as is.

This website runs fine from within Visual Studio, locating and accessing these external DLLs correctly. However, when the website is published on a webserver (runnning IIS6 and ASP.NET 2.0) rather than the development PC it cannot locate and access these external DLLs, and I get the following error:

Unable to load DLL 'XYZ.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

The external DLLs are located in the bin directory of the website, along with the managed DLLs that wrap them and all the other DLLs for the website.

Searching this problem reveals that many other people seem to have the same problem accessing external non.NET DLLs from ASP.NET websites, but I haven't found a solution that works.

I have tried the following:

  • Running DEPENDS to check the dependencies to establish that the first three are in System32 directory in the path, the last is in the .NET 2 framework.
  • I put the two DLLs and their dependencies in System32 and rebooted the server, but website still couldn't load these external DLLs.
  • Gave full rights to ASPNET, IIS_WPG and IUSR (for that server) to the website bin directory and rebooted, but website still couldn't load these external DLLs.
  • Added the external DLLs as existing items to the projects and set their "Copy to Output" property to "Copy Always", and website still can't find the DLLs.
  • Also set their "Build Action" property to "Embedded resource" and website still can't find the DLLs.

Any assistance with this problem would be greatly appreciated!

like image 571
Werg38 Avatar asked Dec 05 '08 17:12

Werg38


People also ask

How do I know if a DLL is managed or unmanaged?

To determine whether a DLL (or EXE) is managed or unmanaged, use dumpbin.exe with the /dependents switch. If you see mscoree. dll in the output, then the assembly is a managed assembly.

Why would a DLL fail to load?

If a DLL fails to load, there can be numerous reasons for the error, listed below are the most common. The DLL or referenced DLL is not in a directory specified in the path. The DLL references another DLL that is not present. Hard disk error has corrupted or damaged a DLL file.

What is DLL in asp net?

A DLL is a library that contains code and data that can be used by more than one program at the same time. For example, in Windows operating systems, the Comdlg32 DLL performs common dialog box related functions. Each program can use the functionality that is contained in this DLL to implement an Open dialog box.


2 Answers

This happens because the managed dlls get shadow copied to a temporary location under the .NET Framework directory. See http://msdn.microsoft.com/en-us/library/ms366723.aspx for details.

Unfortunately, the unmanaged dlls do NOT get copied and the ASP.NET process won't be able to find them when it needs to load them.

One easy solution is to put the unmanaged dlls in a directory that is in the system path (type "path" at the command line to see the path on your machine) so that they can be found by the ASP.NET process. The System32 directory is always in the path, so putting the unmanaged dlls there always works, but I would recommend adding some other folder to the path and then adding the dlls there to prevent polluting the System32 directory. One big drawback to this method is you have to rename the unmanaged dlls for every version of your application and you can quickly have your own dll hell.

like image 73
onedozenbagels Avatar answered Oct 14 '22 13:10

onedozenbagels


As an alternate to putting the dll in a folder that is already in the path (like system32) you can change the path value in your process by using the following code

System.Environment.SetEnvironmentVariable("Path", searchPath + ";" + oldPath) 

Then when LoadLibrary tries to find the unmanaged DLL it will also scan searchPath. This may be preferable to making a mess in System32 or other folders.

like image 30
Matt Woodard Avatar answered Oct 14 '22 13:10

Matt Woodard