# BadImageFormatException using Display

**URL:** <https://forum.commonvisionblox.com/t/badimageformatexception-using-display/554>\
**Category:** Programming Questions\
**Created:** [September 17, 2018, 7:38am UTC](https://forum.commonvisionblox.com/t/badimageformatexception-using-display/554 "2018-09-17T07:38:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ngrotz](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/ngrotz/32/477_2.png) [@ngrotz](https://forum.commonvisionblox.com/u/ngrotz)\
**Post date:** [September 17, 2018, 7:38am UTC](https://forum.commonvisionblox.com/t/badimageformatexception-using-display/554/1 "2018-09-17T07:38:32Z")

</div>

Hello,  
I am running into a BadImageFormatException on step 8 of “Getting started with [CVB.Net](http://CVB.Net)” ([Getting Started with CVB.Net](https://forum.commonvisionblox.com/t/getting-started-with-cvb-net/246)). 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 😌

---

<div class="post-metadata">

**Author:** ![illusive](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/illusive/32/61_2.png) [@illusive](https://forum.commonvisionblox.com/u/illusive)\
**Post date:** [September 17, 2018, 1:15pm UTC](https://forum.commonvisionblox.com/t/badimageformatexception-using-display/554/2 "2018-09-17T13:15:18Z")

</div>

🤦‍♂️  
Unfortunately there is a file missing in the [CVB.Net](http://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: [CVFactory.zip](https://forum.commonvisionblox.com/uploads/commonvisionblox/original/1X/33d70a3fca6b780c1f20d332955e5f31c81bcfa1.zip) (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](http://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.

---

<div class="post-metadata">

**Author:** ![windsorperma](https://avatars.discourse-cdn.com/v4/letter/w/4af34b/32.png) [@windsorperma](https://forum.commonvisionblox.com/u/windsorperma)\
**Post date:** [December 9, 2020, 8:29am UTC](https://forum.commonvisionblox.com/t/badimageformatexception-using-display/554/3 "2020-12-09T08:29:27Z")

</div>

> [@ngrotz](#):
>
> System.BadImageFormatException

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#](http://net-informations.com/faq/default.htm) 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.
