我有下面的代碼扔了BadImageFormatException
。它加載的DLL是32位。 Web服務器運行Windows 64位,但應用程序池設置爲啓用32位。有什麼辦法可以讓這個工作?ASP.Net MVC DllImport BadImageFormatException 32bit/64bit
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();
}
}
你編譯它爲32位或任何CPU的? – 2011-06-06 16:11:20
我已經試過並且都不能加載dll – Jon 2011-06-06 16:13:44
什麼是CDCrypt.dll?您必須將您的WP設置爲使用與任何非託管dll相同的「bitness」。如果你有不止一個,他們必須全部匹配。 – Andy 2011-06-06 16:37:34