Wednesday 21 October 2009

Migrating a .net application to a 64 bit Server

Viusual Studio compiles VB and C# to an intermediate language that by default can run on any cpu. The problem comes when the application accesses some unmanaged DLLs. I got the error

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

On Google I found something quite similar http://social.msdn.microsoft.com/forums/en-US/netfx64bit/thread/35b09f74-1d8e-4676-90e3-c73a439bf632/

In this article the error was

errMsg = "System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) at Wcr_.NET_2005.Module1.dllBIN2_iv(Double price_u, Double ex, Double d_exp, Double d_v, Double price, Double rate_ann,...

The correct answer was

"BadImageFormatException" is thrown on you when the application loads a DLL having incompatible bitness, that is a, 64 bit exe tried to load a 32bit DLL or, a 32 bit exe tried to load a 64 bit DLL.In your case, it's probably the first, so please make sure you compile your vb.net exe as 32 bit (X86) and NOT MSIL.Note also that you should stay away from SysWow64 and System folders, they are reserved by the system and should not contain user applications/DLL's, user stuff must reside under "program files" or "program files (X86)" in an another private folder.So I would suggest you delete all this from the system folders, and move the DLL and the exe to a private folder under "program files (X86)". The lib file has nothing to do with this as it should have beed statically linked when the dll was build.

I solved the problem by following these steps

1. Goto the solution properties
2. Open the Configuration Properties tress and press the configuration Manager button
3. Change Active solution platform from Any CPu to
4. Change Type from Itanium to x86

I ran some tests and my first impression is that the DLL seems to be running much slower than on a 32 bit OS, but I am not sure if this is because of virtuel instead of physical hardware or if it is a problem with the 32 bit mode of a Windows 2003 64 bit OS.