System.BadImageFormatException Error

When deploying an application using one or more SocketTools .NET classes, an unhandled System.BadImageFormatException error occurs with the message "An attempt was made to load a program with an incorrect format (exception from HRESULT: 0x8007000B)".

This error occurs when a SocketTools .NET class attempts to load the incorrect version of the native interop library. Because SocketTools installs the 32-bit and 64-bit versions of the interop library on your development system, it is unlikely you'll encounter this error during development and testing. Usually it only occurs after you attempt to redistribute your application to another system.

In addition to the managed .NET assemblies which are used by your application, SocketTools also uses an interop library which provides the lower-level networking and encryption functionality used by the SocketTools classes. Although the classes themselves are platform neutral, the interop library comes in native x86 (32-bit) and x86-64 (64-bit) versions which also must be installed on the target system.

This error occurs when the 64-bit version of the .NET Framework attempts to load the 32-bit version of the SocketTools interop library, or if the 32-bit version of the framework attempts to load the 64-bit library. Usually this is the result of the installer package not copying the correct version of the library, or selecting the incorrect version from the wrong source path.

If you are building your application on 64-bit Windows and it targets "Any CPU", your installer must include both the 32-bit and 64-bit versions of the interop library. If you are explicitly targeting the x86 platform, then you only need to redistribute the 32-bit interop library. If your application targets the x64 platform, you will need to redistribute the 64-bit version of the interop library.

Redistributable versions of the SocketTools .NET interop libraries can be found in:

C:\Program Files (x86)\SocketTools 11.0 .NET Edition\Redist\x86  (32-bit)
C:\Program Files (x86)\SocketTools 11.0 .NET Edition\Redist\x64  (64-bit)

Be careful when referencing the interop libraries which are installed by SocketTools into your system folders. On 64-bit Windows, the 64-bit version of the interop library is installed in C:\Windows\System32 and the 32-bit version is installed in C:\Windows\SysWOW64. Although this can appear to be backwards at first glance, those are the correct locations for 64-bit and 32-bit libraries and components.

A common mistake is to configure your installer to use the SocketTools11.Interop.dll in the C:\Windows\System32 folder for an application which targets the x86 platform. If you do this, your 32-bit application will attempt to load a 64-bit library, and this will throw an unhandled exception.

To help simplify the redistribution process, we provide MSI installer packages which you can include with your installation project and deploy on the end user's system.

For more information, refer to our article on redistributing .NET applications.

See Also

SocketTools 11 .NET Redistribution
System.DllNotFoundException Error

Shopping Cart
Scroll to Top