Avoiding BadImageFormatException when there is unmanaged code

3

I have a web service that throws a BadImageFormatException any time I try to run the service from Visual Studio. This answer to another question suggested running peverify against DLLs to see if there are any problems. While my web service's DLL is fine, the web service depends on an ILMerged assembly, which does have errors:

[IL]: Error: [C:\blah\MyILMergedAssembly.dll : Encryption

Utils.SecureStringExtensions::SecureEquals][offset 0x00000055][found unmanaged pointer] [expected unmanaged pointer] Unexpected type on the stack.

[IL]: Error: [C:\blah\MyILMergedAssembly.dll : Encryption

Utils.SecureStringExtensions::SecureEquals][offset 0x0000005D][found unmanaged pointer][expected unmanaged pointer] Unexpected type on the stack.

[IL]: Error: [C:\blah\MyILMergedAssembly.dll : Encryption

Utils.SecureStringExtensions::SecureEquals][offset 0x0000007E] Unmanaged pointers are not a verifiable type.

3 Error(s) Verifying .\MyILMergedAssembly.dll

(188 Warnings)

I think these stem from my use of Marshal and IntPtr in MyILMergedAssembly, but everywhere I do that, I have the method declared as unsafe. How can I get rid of the errors peverify gives, and hopefully get rid of the BadImageFormatException that comes up when I try to run my web service?

Edit: it looks like maybe I can't use ILMerge to merge assemblies that have unsafe code. So I tried having my web service reference all the individual DLLs in MyILMergedAssembly, but I still get a BadImageFormatException. Now it gives me the exception on one of the individual assemblies. When I run peverify on that individual assembly, I get:

[IL]: Error: [C:\blah\Connection.dll : Connection

.ConnectionBase::.ctor] [HRESULT 0x80070002] - The system cannot find the file specified.

The ConnectionBase class to which it's referring is right there in Connection.dll, or at least it should be since the class is defined in the Connection namespace/project. Connection has 'Allow unsafe code' checked in its Build options. The constructor in ConnectionBase to which it might be referring is marked as internal.

Edit: I've tried checking 'Allow unsafe code' in the web service project that uses the DLLs with unsafe code. I also tried adding compilerOptions="/unsafe" to the Web.config file for the web service in the system.codedom > compilers > compiler node. I still get a page with the following when I run the web service, though:

Could not load file or assembly 'Connection' or one of its dependencies. An attempt was made to load a program with an incorrect format.

.net
unmanaged
ilmerge
badimageformatexception
peverify
asked on Stack Overflow May 10, 2011 by Sarah Vessels • edited Jun 20, 2020 by Community

1 Answer

1

Oh man, this is annoying. It wasn't unsafe code, it wasn't the 32/64-bit target platform settings on my projects in VS, it wasn't ILMerge. I had to set the Enable 32-Bit Applications setting to True on the application pool in IIS 7 (I'm in Windows 7 64-bit). After restarting the app pool, my web service loads fine. Thanks to this blog for sending me in the right direction.

answered on Stack Overflow May 10, 2011 by Sarah Vessels

User contributions licensed under CC BY-SA 3.0