Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClickOnce: BadImageFormatException when running x86 package on 64 bit windows

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?

like image 436
Marek Avatar asked Oct 15 '22 12:10

Marek


1 Answers

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.

like image 57
Marek Avatar answered Oct 19 '22 00:10

Marek