BadImageFormatException Errors

I've been playing with Lua Programming/Script Language and LuaInterface, because I have a need for embedding a scripting language in one of our projects.  Unfortunately, when trying to run one of the test case applications that came with LuaInterface, I was presented with a nice error shown below:


Anytime, you run a .NET application, the operating system reads the PE header information of the executable to help setup the process environment.  In my case, TestLua.exe was being setup and ran in x64 mode with an x64 runtime and failed to P/Invoke Lua's native 32-bit DLL.


In general, there are two main ways we can explicitly tell the operating system to load and use 32-bit process environments:

For applications and assemblies that you can not recompile:

  • Set the 32-Bit flag in the PE header. 

    For example, since the 32-bit flag in TestLua.exe was not set, I forced 32-bit mode by setting the 32-bit flag using CorFlags.exe:

    Start "Visual Studio SDK Command Prompt" and execute:
    CorFlags.exe /32bit+ TestLua.exe

For applications and assemblies that you can recompile:

  • Recompile using the "Target Platform" settings as illustrated here.

In practice, if you do low-level COM, P/Invoke, Win32 stuff, you'll probably need to set these 32-bit flags for your applications; however, if your application purely lives in the managed world, you won't need to do anything!

Filed Under [ Tips & Tricks Errors ]