-2

The application and all of its libraries are compiled with Platform AnyCPU and Prefer32Bit off. The application loads its libraries from embedded resources, using the currentDomain.AssemblyResource event.

The application is deployed to over one hundred PCs, running Windows 7, Vista, 10, 2008 and 2012. But on some Windows 7 Enterprise 64-bit PCs (not all of them), when the application loads the embedded class libraries the BadImageFormatException is thrown. The JIT compiler is somehow resolving on those PCs to compile the class library to 32 bit instead of 64 and since the hosting application was compiled to 64 bit the exception is thrown.

What other factors will the JIT compiler use in addition to the ones listed above (OS 64 bit, AnyCPU and Prefer32Bit off) to decide in only a few PCs to select 32 instead of 64 bit when loading the class library?

Jeffrey
  • 19
  • 3
  • 1
    If your app fails on 32 bits because of an external reference (as BadImageFormatException suggest), then why don't you just compile for 64 bits only? – Gerardo Grignoli Aug 16 '16 at 01:41
  • The application is deployed to hundreds of PCs with all sort of Windows versions and a mix of 32 and 64 bits. The application is distributed and kept up to date automatically and so far AnyCPU has been working flawless, until recently that it was deployed to a customer that has Win 7 Enterprise 64-bit. In our lab we installed a fresh copy of Win 7 Enterprise and it works fine. So there is something on their Win 7 installation that it causes the JIT compiler to translate one of the class libraries to 32 bits instead of 64. – Jeffrey Aug 17 '16 at 04:38

1 Answers1

0

One possibility is that .NET-64 is disabled on an x64

From: https://stackoverflow.com/a/14857294/97471

There's a setting that can force AnyCPU assemblies to run as 32-bit on x64 OS. Use ldr64.exe from .Net2 x64 directory to check the status:

C:\Windows\Microsoft.NET\Framework64\v2.0.50727>ldr64.exe query loading kernel32...done. retrieved GetComPlusPackageInstallStatus entry point retrieved SetComPlusPackageInstallStatus entry point Current status is: 0x00000001 1 - means 'run AnyCPU as 64-bit' 0 - means 'run AnyCPU as 32-bit'

Though I didn't find such utility in .Net v4 folder, the setting applies to Net4 AnyCPU assemblies as well. This flag is saved in DWORD registry value Enable64Bit under the key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft.NETFramework

This setting seems to be loaded on OS start, and changing only registry value doesn't affect applications until reboot. Changing the flag with ldr64.exe affects immediately.

Note that this setting is system-wide. By default Enable64Bit is set to 1. It seems that some application reset it to 0, and reverting the value back can cause problem to that app.

Community
  • 1
  • 1
Gerardo Grignoli
  • 14,058
  • 7
  • 57
  • 68