简体   繁体   中英

C++ CLI DLL not being loaded by IIS7 but only if built with anything newer than VS2008

For my development environment I'm using Windows 7 Ultimate x64, with VS2008, V2010 and VS2013, and my application is written in C#.

The remote test server has Windows Web Server 2008 R2 x64 and IIS 7.

Here is my specific issue:

I have a native C++ library that I want to use in my C# applications, so I wrote a C++ CLI/CLR wrapper around it, and compiled it all into the same DLL. I've tried both compiling the C++ source into the DLL, and also statically linking the native library into the DLL, and both give me the same result as described below.

If I build this DLL using x64 and .NET 4.5.1 with VS2013, then this DLL works fine with any C# Winforms application in and out of the development environment. It also works when referenced in an ASP.NET application within my development environment (using x64 IIS Express to test). It does NOT work when the web application is deployed (as an x64 build) to the Win2K8 IIS7 server, displaying an BadImageFormatException. I do not build for "AnyCPU" because of the CLI wrapper DLL, which needs to be built for a specific platform.

I have also tried the .NET4.5.1 x86 version of the CLI DLL (using VS2013), and it also works fine with all Winforms applications, and works with ASP.NET on my development machine (using x86 IIS Express), but it does not work on the Win2K8 IIS7 server (x86 build). Same BadImageFormatException error when trying to load this DLL.

On the server it uses an integrated application pool because of the ASP.NET Identity code (won't let me use Classic mode). Also, the "32-bit applications" setting has been toggled to both True/False, and neither has helped solve this issue.

If I build the C++ CLI DLL for .NET 2 or .NET 3.5 and the x86 platform with VS2008, or even VS2013, but selecting VS2008 (V90) as the platform toolset, then it works perfectly fine on the Win2K8 IIS7 server, with no BadImageFormatException error. The ASP.NET integrated app pool "32-bit applications" setting is "True" in order for this to work.

If I build the C++ CLI DLL as .NET 2 or 3.5, x86 platform, but with anything newer than the VS2008 toolset, it fails to load on the IIS server, with BadImageFormatException, even though it works fine on the development machine ASP.NET and in any Winforms application.

Using VS2008, .NET 2 or 3.5, and platform x64, the DLL works in any Winforms app, works with ASP.NET/IIS Express x64 on the development machine, but does NOT work on the Win2K8 IIS7 server, displaying BadImageFormatException. The application pool "32-bit applications" was set to both True and False for testing.

So, in summary, the only option that works on IIS7 is building with the x86 platform selected, using the VS2008 (V90) toolset.

Has anyone experienced anything like this or have an idea what may be causing this issue. I can't see anything drastically different between the working DLL and the others using JetBrains dotNetPeek 1.1, but that's only really a look at the surface... all the code exists in there. Could there be an option in the V100 or higher toolset that is embedding something in the DLL (or leaving something out of it) that is causing it to not be loaded on IIS7?

Any help or suggestions would be appreciated.

Ok, so after struggling with this issue for almost a month now, and resorting to asking on here last night, I came across these links:

Identifying problematic dependencies in C++/CLI project and http://www.codeproject.com/Articles/442784/Best-gotchas-of-Cplusplus-CLI

While they didn't actually specify the solution, there was something in there that I hadn't tried yet, which was to check if the Windows 2008 R2 server required the VS2013 C++ redistributable packages.

I installed both the x86 and the x64 redistributable packages, and suddenly my x64 ASP.NET app that uses the managed C++ wrapper for the native C++ library came to life. Everything is working as intended now. I can compile the wrapper DLL with the VS2013 toolset, x64 platform, using v4.5.1 of .NET, and it works perfectly fine. I no longer require the VS2008 toolset.

So much for BadImageFormatException throwing me off completely. I wish the error displayed on the Win 2K8 IIS7 server was actually more specific, but then again maybe that is just me asking for too much.

I hope this helps someone else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM