ASP.NET run C++/CLI with MFC Dependencies

I had an ABSOLUTE nightmare of a time trying to get ASP.NET to run a DLL that is a C++/CLI on a native C++ MFC class library. You may have errors like this:


[FileNotFoundException: The specified module could not be found. (Exception from HRESULT: 0x8007007E)]



This PAINFULLY generic error doesn't tell you anything. But what it is trying to say is that the DLL you are trying to load is missing another DLL that it needs to run. When trying to interop with C++/CLI, you could see this error. This is especially common when you run your code from your local Visual Studio installed machine and it works perfectly. Then you deploy the code to a remote server and suddenly it doesn't work, even though you have the exact same files! Here are a couple of things to keep in mind in dealing with this error:

1) Use Depends.exe (http://www.dependencywalker.com/ application to load your DLL. It will show you exactly what DLL's it relies on so you can make sure they are available and configured.

2) You may think that ASP.NET loads all libraries and dll's from the Bin folder but that is not entirely true. There is an excellent blog post here that describes in detail how and in what order ASP.NET looks for native dependent dll's. (http://blogs.msdn.com/jorman/archive/2007/08/31/loading-c-assemblies-in-asp-net.aspx).

3) In many cases, installing the CORRECT version of the C++ Run-time files will solve your issue. NOTE: You have to make sure the run-time you have installed is exactly the run-time your application depends on. If you just copy and paste dll's such as for MFC into your Bin folder, you will not have success (see note number 2 above for details). You have to install them so ASP.NET will find them at run-time. Here are some links to the redistributables for these files:

Microsoft Visual C++ 2005 Redistributable Package (x86)

http://www.microsoft.com/downloads/details.aspx?familyid=32BC1BEE-A3F9-4C13-9C99-220B62A191EE&displaylang=en

Microsoft Visual C++ 2005 SP1 Redistributable Package (x86)

http://www.microsoft.com/downloads/details.aspx?familyid=200B2FD9-AE1A-4A14-984D-389C36F85647&displaylang=en

Microsoft Visual C++ 2008 Redistributable Package (x86)

http://www.microsoft.com/downloads/details.aspx?FamilyID=9B2DA534-3E03-4391-8A4D-074B9F2BC1BF&displaylang=en

Microsoft Visual C++ 2008 SP1 Redistributable Package (x86)

http://www.microsoft.com/downloads/details.aspx?familyid=A5C84275-3B97-4AB7-A40D-3802B2AF5FC2&displaylang=en

4) If you are doing a C++/CLI wrapper on a native dll, then you need to make sure you are "delay loading" dependent assemblies. This took me forever to figure out but totally solved my problem. In Visual Studio 2008, Right-click your C++/CLI project and select Properties. Then tree open Configuration Properties -> Linker -> Input. In the "Delay Loaded DlLs" setting, put in the exact DLL name(s) of the dll's that this C++/CLI assembly relies on. Click OK and rebuild and you should be good to go.

5) Make sure you are compiling/building/running your dll's in Release configuration. If you run in debug, then those C++ redistributable libraries you installed will be worthless because they only install release dll's.

Thanks

Thanks a lot for this post!
I was developing some wrappers over native dlls with C++/CLI and the got the same error with ASP.NET but point N4 resolved the problem.