0

I have a 64bit web application written in c# deployed to IIS on both windows server 2008 (IIS 7) and windows server 2012 (IIS 8).

Part of the application involves accessing an unmanaged c++ DLL from c# code. This call is failing when I deploy to IIS. I get the classic:

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

I have read many threads on the topic, including:

An attempt was made to load a program with an incorrect format ERROR

“An attempt was made to load a program with an incorrect format” even when the platforms are the same

But I don't think they apply as

  1. I have a 64 bit application.
  2. It's compiled as x64
  3. It's deployed as x64
  4. I have verified it is running under an x64 IIS appPool.
  5. enable32bitapplications is set to false.
  6. Both the c# dll importing and the unmanaged dll are compiled to 64bit.
  7. I have deployed to IIS on the server machines in both Release and Debug mode - no difference.

There are 32 bit dlls in the application, but they are in completely separate projects which are not referenced by, nor reference, the project that is throwing the error.

For additional information, it runs fine on both my local machine in IIS express AND my local machine when deployed through IIS (windows 7), so there is a disconnect somewhere and I can't track it down.

Additional info:

The unmanaged DLL does have dependencies on:

  • Kernel32.dll
  • Advapi32.dll
  • Crypt32.dll
  • User32.dll
  • Version.dll

I realize these are 32bit I thought these were 32bit, but on a 64 bit machine these live in System32, so I'm not sure... but if so, how can I get it to compile as the unmanaged DLL itself is compiled to 64 bit.

Also, why would it work locally for me?

Also, I have (just for kicks and skittles) set Enable32BitApplications to True and I get the same error, as my project is a 64 bit project and references a 64bit unmanaged DLL, which can't run in a 32 bit process.

Community
  • 1
  • 1
Blunderfest
  • 1,854
  • 1
  • 28
  • 46

2 Answers2

1

There was an additional dependency for the unmanaged DLL that was a c++ redistributable dll. The copy of that DLL on the machine was 32 bit, once I swapped out the version to be 64 bit, it loaded just fine.

Lesson learned, all dependencies of the DLL must be present and the correct Bitness.

Blunderfest
  • 1,854
  • 1
  • 28
  • 46
-2

You need to set Enable32bitApplications to true in IIS.

kmacdonald
  • 3,381
  • 2
  • 18
  • 22
  • You can run a 64x bit application with 32bitApplicationEnabled set to true. – kmacdonald Jul 13 '15 at 21:23
  • At least try it. If it runs successfully you have a referenced 32 bit DLL and the application will not run unless you set this flag to true. hence the error.... – kmacdonald Jul 13 '15 at 21:27
  • You cannot load a 32-bit DLL into a 64-bit process. – IInspectable Jul 13 '15 at 21:28
  • Im sorry you are correct. By default IIS will only support 64bit applications. you need to explicitly enable 32 bit applications these days. However, i would still say that a 32bit DLL is attempting to be loaded :) – kmacdonald Jul 13 '15 at 22:01
  • Turns out you were half correct. A 32 bit DLL was attempting to be loaded, but I needed to switch out the DLL, not enable 32 bit applications. – Blunderfest Jul 14 '15 at 17:40
  • @Blunderfest: No, he wasn't half correct. No matter what you try, you cannot load a 32-bit DLL into a 64-bit process, as was [mentioned before](http://stackoverflow.com/questions/31393211/deployed-64bit-c-sharp-application-cant-load-unmanaged-dll/31393636#comment50763893_31393636). – IInspectable Jul 14 '15 at 21:14
  • Half correct meaning, a 32 bit dll was attempting to be loaded :). – Blunderfest Jul 14 '15 at 21:25
  • @Blunderfest: Uhm, that wasn't him. It was me, [about a day ago](http://stackoverflow.com/questions/31393211/deployed-64bit-c-sharp-application-cant-load-unmanaged-dll/31393636#comment50763608_31393211). And that wasn't half correct. It was 100% on target. – IInspectable Jul 14 '15 at 21:39
  • Four comments up: " However, i would still say that a 32bit DLL is attempting to be loaded :) ". But we digress. And yes, you were correct. – Blunderfest Jul 15 '15 at 14:39