Monthly Archives: March 2009

DllImport 32bit dlls in .net assemblies throws BadImage exception on 64bit OS

When you compile your .Net executable with “AnyCPU” platform, it will select the “CLR” at runtime depending on the currently running platform.
Which means that your application will run as a 64-bit application on x64 machine and as 32-bit application on x86 machine. On x64 machines if you use any “DllImport” in your .Net executable to load a 32-bit native dll, while you compile your executable with “AnyCPU” platform, it will crash at runtime because no 64-bit application can run 32-bit dll.
So, if you want to load a 32-bit native dll with DllImport in a .Net executable you have to compile this .Net executable for “x86” platform to make this executable run under the WOW64 as a 32-bit application this will make it capable to load native 32-bit dlls