BadImageFormatException using Display

I am running into a BadImageFormatException on step 8 of “Getting started with CVB.Net” (Getting Started with CVB.Net). I made sure that “Prefer 32-Bit” is unchecked and that the correct references are added. However, every time the “Display” from the Toolbox is added, Visual Studio throws the following exception:

Failed to create component 'Display. The error message follows:
‘System.BadImageFormatException: Es wurde versucht, eine Datei mit falschem Format zu laden. (Exception from HRESULT: 0x8007000B) at Stemmer.Cvb.Forms.DisplayLib.CreateDisplay(IntPtr&Handle) at Stemmer.Cvb.Forms.Controls.Display…ctor()’

I was wondering, if anyone else is familiar with this or maybe knows, what I am doing wrong.
Thank you :relaxed:

Unfortunately there is a file missing in the CVB.Net installer (seems like a new dependency crept in and the installer was not updated accordingly). The installer will be updated in due time - in the meantime you can just drop the attached CVFactory.dll to the C:\Windows\SysWOW64 folder and the exception should be gone: (223.5 KB).

For more detail klick the blurred text…

As Visual Studio is always a 32 bit process, it will instantiate the Forms Display from the new CVB.Net API as a 32 bit entity. This display relies on an implementation inside the native CVCDisp.dll - which would not generally be present in a 64 bit installation of :cvb:. Therefore the Bindings Preview installer installs the CVCDisp.dll and - ideally… - all its dependencies to C:\Windows\SysWOW64 where the operating system will be able to find it. If, however, a dependency is missing the DLL resolution will fail to load the CVCDisp.dll from C:\Windows\SysWOW64 and continue searching for an appropriate CVCDisp.dll. It’ll sooner or later encounter the one located in %CVB% and - for lack of a better candidate - try to load it, realize that it’s a 64 bit entity - which causes the System.BadImageFormatException that you’ve seen.

1 Like

The exception that is thrown when the file image of a dynamic link library (DLL) or an executable program is invalid. If you get a BadImageFormatException when interfacing with a native DLL, it almost always means that you are trying to interface with a 32-bit DLL while running in the 64-bit CLR, or vice versa. In most cases you might be facing the problem with your website after deploying on server.

Make sure that you are not having 32-bit / 64-bit conflict. So, you need to adjust your application pool to Enable 32-Bit or 64-Bit accordingly. Set the Target platform setting on your c# EXE project, not your class library project. Alternatively, you can ship both 32-bit and 64-bit DLLs with different file names, define separate P/Invoke stubs for each version, and decide which one to call at runtime. The easiest way to do this would probably be to wrap your native calls in an interface (e.g., INativeMethods) and choose which implementation to instantiate at runtime based on IntPtr.Size. With this method, you could still target AnyCPU.