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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: