.Net executable with C++/CLI layer gets System.BadImageFormatException

1k views Asked by At

This problem seems to be pretty common to anyone dealing with c++/cli, but I'm missing something with the typical resolution path. I have a .Net assembly, PHD.exe, that has a reference to a c++/CLI dll. This DLL dynamically links a number of win32 native dlls.

Exception text:

Main Window Exception: The type initializer for 'X.BLCInterfaceBehavior' threw an exception. System.BadImageFormatException: Could not load file or assembly 'CppCLI.dll' or one of its dependencies. is not a valid Win32 application. (Exception from HRESULT: 0x800700C1)

Following other recommendations on SO, I check the target type for my executable. Right click > Properties > Build Tab > Platform = x86. Platform Target: x86.

Likewise, I check the project properties for the c++/cli dll ( I hope this is the correct place to look): Properties > Configuration Properties > Linker > Target Machine = MachineX86

I ran DependencyWalker on the CppCLI.dll, and get the following errors:

Error: At least one module has an unresolved import due to a missing export function in an implicitly dependent module.

Error: Modules with different CPU types were found. Warning: At least one module has an unresolved import due to a missing export function in a delay-load dependent module.

I'm not sure how to identify which DLL is missing an expected export function, or if this is just one of this depends.exe quirks on an x64 system. Same thing applies to the modules with diff cpu types - I think I have seen "ignore that" message here on SO.

I ran the app against winDBG, and see that some windows DLLs are loading from SysWOW64 - this seems like its wrong. Some examples:

ModLoad: 75880000 75910000 C:\Windows\syswow64\GDI32.dll

ModLoad: 753f0000 753fa000 C:\Windows\syswow64\LPK.dll

ModLoad: 76c60000 76cfd000 C:\Windows\syswow64\USP10.dll

ModLoad: 76d00000 76da0000 C:\Windows\syswow64\ADVAPI32.dll

ModLoad: 75fe0000 76c2a000 C:\Windows\syswow64\SHELL32.dll

Shouldn't these be loaded from System32?

Finally WinDBG shows the exception, but I don't see anything useful:

ModLoad: 6dfc0000 6e060000 GambitManagedWrapper.dll

ModLoad: 01270000 01310000 GambitManagedWrapper.dll

ModLoad: 72eb0000 72ebe000 C:\Windows\SysWOW64\RpcRtRemote.dll

ModLoad: 6dfc0000 6e060000 C:\Gambit\GambitManagedWrapper.dll

(fbc.ebc): C++ EH exception - code e06d7363 (first chance)

(fbc.ebc): C++ EH exception - code e06d7363 (first chance)

(fbc.ebc): C++ EH exception - code e06d7363 (first chance)

(fbc.ebc): C++ EH exception - code e06d7363 (first chance)

(fbc.ebc): C++ EH exception - code e06d7363 (first chance)

(fbc.ebc): CLR exception - code e0434352 (first chance)

(fbc.ebc): C++ EH exception - code e06d7363 (first chance)

(fbc.ebc): CLR exception - code e0434352 (first chance)

(fbc.ebc): CLR exception - code e0434352 (first chance)

Additionally, I ran this command in winDBG, which further confuses things for me:

effmach # Effective machine: x86 compatible (x86)

Not really sure where to go from here, and would appreciate any and all suggestions.

0

There are 0 answers