ASP.Net MVC DllImport BadImageFormatException 32bit / 64bit
我有下面的代码抛出BadImageFormatException
。 它加载的DLL是32位。 Web服务器正在运行Windows 64位,但应用程序池设置为启用32位。 有什么方法可以让这个工作吗?
public class HomeController : Controller { [DllImport("CDCrypt.dll")] [return: MarshalAs(UnmanagedType.LPStr)] public static extern String Encrypt ([MarshalAs(UnmanagedType.LPStr)] String aName); [DllImport("CDCrypt.dll")] [return: MarshalAs(UnmanagedType.LPStr)] public static extern String Decrypt ([MarshalAs(UnmanagedType.LPStr)] String aName); public ActionResult Index() { try { ViewBag.EncryptString = Encrypt("test"); } catch (Exception e) { ViewBag.EncryptString = "Stack Trace\r\n:" + "\r\nException: " + e.Message; return new HttpStatusCodeResult(500); } return View(); } public ActionResult Up() { ViewBag.Up = "You can see me"; return View(); } }
图书馆和消费者都必须是一样的。 因此,确定您正在调用的lib是什么,并确保您正在为该类型进行编译。 现在 – IIS本身(至少v 6)只能运行一种模式或另一种模式,因此需要将其设置为64或32位。 请参阅http://support.microsoft.com/kb/894435
和这个结合: