Troubleshooting: BadImageFormatException in .NET applications

Last Reviewed: August 6, 2013

Article: DTS0223

Applies to: dtSearch Engine .NET API

Symptom:  "An attempt was made to load a program with an incorrect format" message or System.BadImageFormatException running a .NET program on a 64-bit version of Windows

"An attempt was made to load a program with an incorrect format" means that you are trying to load a DLL that is mismatched with the platform.  Specifically, there is a 32-bit vs. 64-bit issue.  

Resolution 1.  Use the 64- version of the dtSearch Engine's .NET API under 64-bit Windows

For information on using the 64-bit version of the dtSearch Engine, see How to use the 64-bit version of the dtSearch Engine.

If you get this error using the 64-bit version of the dtSearch Engine API, set your .NET project's "Platform Target" (in the Build tab of the project Properties) to x64 instead of "Any CPU".

For ASP.NET applications, you may need to set up the project to conditionally use either the 32-bit or the 64-bit version of the dtSearch .NET API, depending on the environment, because of the way Visual Studio builds ASP.NET applications.  For more information, see How to use the 64-bit version of the dtSearch Engine.

Resolution 2.  Mark your application as 32-bit

To use the 32-bit version of the dtSearch Engine on a 64-bit server, you can set some flags in the .exe to let .NET know to run the assembly as 32-bit and not 64-bit.  

One way to do this is to change the Platform Target (in the Build tab of the project Properties) to x86 instead of "Any CPU".

Alternatively, you can use corflags.exe (a utility included with the .NET SDK) to set ILONLY- and 32BIT+ on your assembly.

For documentation on working with corflags, see:

CorFlags Conversion Tool
http://msdn2.microsoft.com/en-us/library/ms164699(VS.80).aspx

For ASP.NET applications, this article explains how to configure 64-bit IIS to run 32-bit processes:

http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/13f991a5-45eb-496c-8618-2179c3753bb0.mspx?mfr=true

If the ASP.NET application is called as a 32-bit process, ASP.NET should build it as a 32-bit executable, and then dten600.dll and dtSearchNetApi2.dll will load correctly.