ASP.Net MVC DllImport BadImageFormatException 32bit / 64bit

I have the code below that throws BadImageFormatException. DLL download is 32 bits. The web server is running Windows 64bit, but the application pool is set to Enable 32 bits. Is there any way to make this 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();
    }
}
+1
source share
1 answer

Both the library and the consumer must be the same. Therefore, determine what exactly you are calling and make sure that you are compiling for this type. Now - IIS itself (at least v 6) can run only one mode OR another for everything, so it either needs to be set for 64 or 32 bits all over the board. See this http://support.microsoft.com/kb/894435

in combination with:

http://social.msdn.microsoft.com/Forums/en-US/netfx64bit/thread/35b09f74-1d8e-4676-90e3-c73a439bf632/

+1

All Articles