ASP.Net MVC的DllImport BadImageFormatException 32位/ 64位 [英] ASP.Net MVC DllImport BadImageFormatException 32bit/64bit
问题描述
我有低于code这是抛出一个 BadImageFormatException
。它的加载DLL是32位。 Web服务器运行的是Windows 64位,但应用程序池设置为启用32位。有什么办法,我可以得到这个工作?
公共类HomeController的:控制器
{
函数[DllImport(CDCrypt.dll)]
[返回:的MarshalAs(UnmanagedType.LPStr)
公共静态外部加密字符串
([的MarshalAs(UnmanagedType.LPStr)]字符串aName); 函数[DllImport(CDCrypt.dll)]
[返回:的MarshalAs(UnmanagedType.LPStr)
公共静态外部字符串解密
([的MarshalAs(UnmanagedType.LPStr)]字符串aName); 公众的ActionResult指数()
{
尝试
{
ViewBag.EncryptString =加密(测试);
}
赶上(例外五)
{
ViewBag.EncryptString =
堆栈跟踪\\ r \\ n+\\ r \\ n异常:+ e.Message;
返回新的HTTPStatus codeResult(500);
}
返回查看();
} 公众的ActionResult向上()
{
ViewBag.Up =你可以看到我;
返回查看();
}
}
无论是库和消费者都必须相同。因此,确定您的lib您呼叫的,并确保您编译该类型。现在 - IIS本身(至少6节)只能运行一种模式或其他为的所有的,因此无论是需求全线64位或32位进行设置。看到这个
http://support.microsoft.com/kb/894435
会同:
<一个href=\"http://social.msdn.microsoft.com/Forums/en-US/netfx64bit/thread/35b09f74-1d8e-4676-90e3-c73a439bf632/\" rel=\"nofollow\">http://social.msdn.microsoft.com/Forums/en-US/netfx64bit/thread/35b09f74-1d8e-4676-90e3-c73a439bf632/
I have the below code which is throwing a BadImageFormatException
. The DLL it's loading is 32bit. The web server is running Windows 64bit but the application pool is set to Enable 32 bit. Is there any way I can get this to work?
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();
}
}
Both the library and the consumer have to be the same. So determine what your lib you are calling is and ensure you are compiling for that type. Now - IIS itself (at least v 6) can only run one mode OR the other for everything so it either needs to be set for 64 or 32 bit across the board . See this http://support.microsoft.com/kb/894435
in conjunction with:
这篇关于ASP.Net MVC的DllImport BadImageFormatException 32位/ 64位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!