views:

686

answers:

2

My .NET 2.0 application imports unmanaged 32 bit dll. The dll is loaded (first interop call happens) when user opens a file via a dialog within the application.

When I deploy the application via clickonce with target platform "Any", users on 64 bit windows get BadImageFormatException when trying to open files from the application (at the moment the unmanaged dll is loaded). I understand this is due to incompabible bitness of the 64 bit process and the 32 bit unmanaged dll.

I have redeployed the application using x86 as target platform. As I understand it, this should solve the bitness problem.

BUT

When I run the deployed application built for x86 on 64 bit system, I now get BadImageFormatException immediately before the application even starts. Tested on at least three 64 bit machines. On 32 bit machines, it works with no problem.

When I run the application directly from VS (or not directly, just a normal build, not going via ClickOnce), there is no problem on 64 bit windows when using x86 target platform. The application starts and user can load file - the interop call succeeds.

I have been debugging this for 2 days straight with no result - I have tried on different computers. It seems to consistentnly work on one of the computers I have tried. However, I do not have permanent access to this computer.

I have managed to build the ClickOnce deployment on my computer once and it worked on a 64 bit machine. This was single of maybe 100 tries! Nothing has changed, the only changed variable was that I did the successful build immediately after computer restart.

I did clean/rebuild/restart VS/restart windows MANY times. I have reinstalled VS 2008 and now also the whole OS, it did not help.


EDIT: I have just managed to get one good build (out of next 100 :)) and did comparison between the deployed directories. The source of the problem is that ClickOnce generates the wrong target platform in the manifest of the main .exe:

<asmv1:assemblyIdentity name="app.exe" version="1.0.4.18" publicKeyToken=".token here." language="neutral" processorArchitecture="<b>msil</b>" type="win32" />

processorArchitecture should be x86.

So the question is how to consistently force VS to generate the correct processorArchitecture in the manifest when deploying.

Can anyone help please?

A: 

Hi Marek, I'd suggest to use Reflector to open the main exe deployed by ClickOnce and see the dependencies to make sure you are not deploying the 64 bit version of the dll by mistake.

Nestor
Thanks for hint, I have already checked. The managed dll deployed and referenced by the main exe has .corflags 0x00000003 // ILONLY 32BITREQUIRED, so it is not the 64 bit version.
Marek
+1  A: 

This was resolved by installing VS 2008 SP1 on 64 bit Windows 7. VS2008 SP1 on XP had the problem on two machines I have tried with.

Marek
@Marek: I am glad that I stumbled on your Q/A, as I have been having similar other troubling issues, which I will test on Monday if they are related to the issue you saw.
maxwellb