Loading Cross platform assemblies (DLLImport)   

 

Well it turns out you can't really load assemblies compiled for other platforms. I got this error during the migration of an application to a x64 bit server.

An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

The application was loading a third party assembly. It turns out that you can't import cross platform assemblies. The dllimport call would fail as the native code was compiled in x32. In my first attempt to fix this I compiled the managed project in a x32 bit machine and tried to install it again. The problem is that MSIL code (the output of the compilation of visual studio) is platform independent, so I would still get the same error.

Compiling the native code (assembly being loaded with dllimport) in x64 bits was not an option so I had to turn to Google. It turns out that you can force the platform for managed code using Corflags.exe. This is a tool that comes with the Windows SDK. The tool can set a 32Bit flag on the assemblies. This flag will force the code to run as 32 bit code.
Here is an example:

CorFlags.exe assembly.dll /32Bit+
I had to make sure that all managed assemblies used had the flag set for the code to work. This is a quick fix as you won't be taking advantage of the x64 platform. In my case it didn't really mattered. I get the feeling that I'll be running into this again as we are finally moving to 64bit operating systems.
Published  on  9/7/2009  by  xsolon
1  Comment  |  Trackback Url  | 0  Links to this post | Bookmark this post with:          
Tags: | Categories:
Technorati Tags:
 

Links to this post

Comments

2010-01-16 14:55:21  by Anonymous
comment

Title:
Receieve Notification:
Website:
Email:
Comments:

CAPTCHA Image Validation