32-bit/64-bit incompatibility fun

So, I’m working on a C#/.NET application which currently uses a 32-bit version of TIBCO RV. The company I’m working for is (slowly) moving from Windows XP over to Windows 7 (and the brave new world of 64-bit computing). However they’re obviously wanting to test their suite of in-house application and ensure they’re all run correctly on the 64-bit machines.

I’ve been tasked with ensuring our application works. Ofcourse immediately we hit a problem which was the use of the 32-bit TIBCO RV. The application, by default, was built with the “Any CPU” configuration. The way the JIT works is that it will JIT compile “Any CPU” code to the architecture of the CPU, so on a 32-bit OS it’s 32-bit and on a 64-bit OS it’s run as a 64-bit application – which is how it obviously should be.

But with the inclusion of the 32-bit DLL and the JIT switching the application to 64-bit obviously caused a bit of a problem and we were seeing the “System.BadImageFormatException” exception when the application attempted to load the 32-bit DLL. Ofcourse this is to be expected as you can’t mix 64-bit and 32-bit in the same application.

Apart from the obvious solution of getting a 64-bit version of TIBCO RV installed (which is ultimately where we’d hope to end up). How do we solve this problem now?

So the key to solving this is the “Any CPU” configuration. If we create an x86 configuration we can force the JIT to build for a 32-bit architecture and x64 will force the code to 64-bit.

Now here I made a subtle mistake which I’m going to share with you. I created a new configuration whereby all projects were switched to “x86” I compiled the application without a hitch and deployed it, only to find it still failing. Part of the problem was I didn’t have a 64-bit machine to run on, so this was a rather slow process of building, deploying and testing :)

To cut a long story short (too late I know). I eventually got some time on a 64-bit machine. I rebuilt the client on this machine in “x86” mode and strangely (unlike building it on the 32-bit machine) I got loads of errors. Seemingly incompatible assemblies all pointing to a bunch of third party UI assemblies (which I assume are built as Any CPU) – now I’ve not confirmed this yet but I wondered at this point whether the problem is to do with some of my assemblies configured as x86 and others as Any CPU. That’s something I need to look into.

Anyway further investigation brought to my attention the following application

corflags /32bit+

I used this on the EXE only and ran the application an it worked perfectly.

Switching back to Visual Studio I changed only the EXE’s project to build x86 code. I recompiled the solution and had no incompatibilities during the build. Then I ran the application and all worked perfectly.

So it appears just setting the entry point (in this case the EXE) to x86 solved the problem, seemingly forcing the application into 32-bit mode.