title: "A first chance exception of type 'System.BadImageFormatException' " after buildint on a 64-bit computer
body: <p>My Visual Studio 2010 solution has C DLL and C# project, which calls C DLL.
  Solution works ok when built on 32-bit Windows-7 Pro computer.
  On 64-bit Windows-7 Pro, builds okay, but when ran, C# calls function in the DLL and gets error:</p>
  
  <pre><code>A first chance exception of type 'System.BadImageFormatException'
  </code></pre>
  
  <p>Solution Configuration Manager settings are:</p>
  
  <pre><code>&lt;DLL project&gt;   Platform = Win32       (&lt;&lt;&lt; no option to change to "Any CPU")
  &lt;C# project&gt;    Platform = "Any CPU"
  </code></pre>
  
  <p>The output below is:</p>
  
  <ol>
  <li><p>C DLL BUILD OUTPUT</p></li>
  <li><p>C# BUILD OUTPUT</p></li>
  <li><p>RUN OUTPUT</p></li>
  </ol>
  
  <p>C DLL BUILD OUTPUT:</p>
  
  <pre><code>1&gt;------ Rebuild All started: Project: Ultrasound_Frame_Grabber, Configuration: Debug Win32 ------
  1&gt;  Processing ..\..\..\include\USGFWSDK\include\Usgfw2.idl
  1&gt;  Usgfw2.idl
  1&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\oaidl.idl
  1&gt;  oaidl.idl
  1&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\objidl.idl
  1&gt;  objidl.idl
  1&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\unknwn.idl
  1&gt;  unknwn.idl
  1&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\wtypes.idl
  1&gt;  wtypes.idl
  1&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\basetsd.h
  1&gt;  basetsd.h
  1&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\guiddef.h
  1&gt;  guiddef.h
  1&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\ocidl.idl
  1&gt;  ocidl.idl
  1&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\oleidl.idl
  1&gt;  oleidl.idl
  1&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\servprov.idl
  1&gt;  servprov.idl
  1&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\urlmon.idl
  1&gt;  urlmon.idl
  1&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\msxml.idl
  1&gt;  msxml.idl
  1&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\strmif.idl
  1&gt;  strmif.idl
  1&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\control.odl
  1&gt;  control.odl
  1&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\oaidl.acf
  1&gt;  oaidl.acf
  1&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\ocidl.acf
  1&gt;  ocidl.acf
  1&gt;  stdafx.cpp
  1&gt;  DIBSectn.cpp
  1&gt;  sample.cpp
  1&gt;  sampleDlg.cpp
  1&gt;  Ultrasound_Frame_Grabber.cpp
  1&gt;  Generating Code...
  1&gt;  Usgfw2_i.c
  1&gt;  .NETFramework,Version=v4.0.AssemblyAttributes.cpp
  1&gt;     Creating library C:\Primary\PROJECTS\VasoNova\WORK\Win7 VPS\Debug\Ultrasound_Frame_Grabber.lib and object C:\Primary\PROJECTS\VasoNova\WORK\Win7 VPS\Debug\Ultrasound_Frame_Grabber.exp
  1&gt;strmbasd.lib(mtype.obj) : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specification
  1&gt;  sample.vcxproj -&gt; C:\Primary\PROJECTS\VasoNova\WORK\Win7 VPS\Debug\Ultrasound_Frame_Grabber.dll
  ========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
  </code></pre>
  
  <p>2) C# BUILD OUTPUT:</p>
  
  <pre><code>1&gt;------ Skipped Rebuild All: Project: VNMath, Configuration: Debug Win32 ------
  1&gt;Project not selected to build for this solution configuration 
  2&gt;------ Skipped Rebuild All: Project: GuiManager, Configuration: Debug Any CPU ------
  2&gt;Project not selected to build for this solution configuration 
  3&gt;------ Rebuild All started: Project: Ultrasound_Frame_Grabber, Configuration: Debug Win32 ------
  4&gt;------ Skipped Rebuild All: Project: VNCommon, Configuration: Debug Any CPU ------
  4&gt;Project not selected to build for this solution configuration 
  5&gt;------ Skipped Rebuild All: Project: AudioUtility, Configuration: Debug Any CPU ------
  5&gt;Project not selected to build for this solution configuration 
  6&gt;------ Skipped Rebuild All: Project: VNDevices, Configuration: Debug Any CPU ------
  6&gt;Project not selected to build for this solution configuration 
  7&gt;------ Skipped Rebuild All: Project: SimpleRPC, Configuration: Debug Any CPU ------
  7&gt;Project not selected to build for this solution configuration 
  8&gt;------ Skipped Rebuild All: Project: VNSockCommunication, Configuration: Debug Any CPU ------
  8&gt;Project not selected to build for this solution configuration 
  9&gt;------ Skipped Rebuild All: Project: PdfSharp, Configuration: Debug Any CPU ------
  9&gt;Project not selected to build for this solution configuration 
  10&gt;------ Skipped Rebuild All: Project: VNUserControls, Configuration: Debug Any CPU ------
  10&gt;Project not selected to build for this solution configuration 
  11&gt;------ Skipped Rebuild All: Project: GuiServer, Configuration: Debug Any CPU ------
  11&gt;Project not selected to build for this solution configuration 
  3&gt;  Processing ..\..\..\include\USGFWSDK\include\Usgfw2.idl
  3&gt;  Usgfw2.idl
  3&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\oaidl.idl
  3&gt;  oaidl.idl
  3&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\objidl.idl
  3&gt;  objidl.idl
  3&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\unknwn.idl
  3&gt;  unknwn.idl
  3&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\wtypes.idl
  3&gt;  wtypes.idl
  3&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\basetsd.h
  3&gt;  basetsd.h
  3&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\guiddef.h
  3&gt;  guiddef.h
  3&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\ocidl.idl
  3&gt;  ocidl.idl
  3&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\oleidl.idl
  3&gt;  oleidl.idl
  3&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\servprov.idl
  3&gt;  servprov.idl
  3&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\urlmon.idl
  3&gt;  urlmon.idl
  3&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\msxml.idl
  3&gt;  msxml.idl
  3&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\strmif.idl
  3&gt;  strmif.idl
  3&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\control.odl
  3&gt;  control.odl
  3&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\oaidl.acf
  3&gt;  oaidl.acf
  3&gt;  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\ocidl.acf
  3&gt;  ocidl.acf
  3&gt;  stdafx.cpp
  3&gt;  DIBSectn.cpp
  3&gt;  sample.cpp
  3&gt;  sampleDlg.cpp
  3&gt;  Ultrasound_Frame_Grabber.cpp
  3&gt;  Generating Code...
  3&gt;  Usgfw2_i.c
  3&gt;  .NETFramework,Version=v4.0.AssemblyAttributes.cpp
  3&gt;     Creating library C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\Debug\Ultrasound_Frame_Grabber.lib and object C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\Debug\Ultrasound_Frame_Grabber.exp
  3&gt;strmbasd.lib(mtype.obj) : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specification
  3&gt;  sample.vcxproj -&gt; C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\Debug\Ultrasound_Frame_Grabber.dll
  12&gt;------ Rebuild All started: Project: MY_APP, Configuration: Debug Any CPU ------
  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\MY_APP\FrmMY_APP.cs(291,4): warning CS0162: Unreachable code detected
  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\MY_APP\FrmMY_APP.cs(441,4): warning CS0164: This label has not been referenced
  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\MY_APP\FrmMY_APP.cs(1276,13): warning CS0219: The variable 'exportFrmIsUp' is assigned but its value is never used
  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\MY_APP\FrmMY_APP.cs(91,66): warning CS0067: The event 'VN.Forms.MY_APPForm.m_eventTabletConnectionStatusChanged' is never used
  
  Compile complete -- 0 errors, 4 warnings
  12&gt;  MY_APP -&gt; C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\MY_APP\bin\Debug\MY_APP.exe
  12&gt;  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\\CommsLibrary\Common.Logging.dll
  12&gt;  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\\CommsLibrary\Common.Logging.Log4Net.dll
  12&gt;  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\\CommsLibrary\DistributedFileSystem.dll
  12&gt;  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\\CommsLibrary\ICSharpCode.SharpZipLib.dll
  12&gt;  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\\CommsLibrary\libMobiledevice.dll
  12&gt;  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\\CommsLibrary\log4net.config
  12&gt;  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\\CommsLibrary\log4net.dll
  12&gt;  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\\CommsLibrary\NetworkCommsDotNet.dll
  12&gt;  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\\CommsLibrary\protobuf-net.dll
  12&gt;  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\\CommsLibrary\QuickLZCompressor.dll
  12&gt;  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\\CommsLibrary\SerializerBase.dll
  12&gt;  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\\CommsLibrary\SevenZipLZMACompressor.dll
  12&gt;  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\\CommsLibrary\SharpZipLibCompressor.dll
  12&gt;         13 file(s) copied.
  ========== Rebuild All: 2 succeeded, 0 failed, 10 skipped ==========
  </code></pre>
  
  <p>3) RUN OUTPUT:</p>
  
  <pre><code>'MY_APP.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'MY_APP.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'MY_APP.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'MY_APP.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'MY_APP.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'MY_APP.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'MY_APP.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'MY_APP.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Primary\PROJECTS\VasoNova\WORK\Win7 VPS\MY_APPG4\bin\Debug\MY_APP.vshost.exe', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'MY_APP.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'MY_APP.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_64\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'MY_APP.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'MY_APP.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Deployment\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Deployment.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'MY_APP.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel\v4.0_4.0.0.0__b77a5c561934e089\System.ServiceModel.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'MY_APP.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.ServiceProcess\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.ServiceProcess.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'MY_APP.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.Extensions\v4.0_4.0.0.0__31bf3856ad364e35\System.Web.Extensions.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'MY_APP.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  The thread 'vshost.NotifyLoad' (0x197c) has exited with code 0 (0x0).
  The thread '&lt;No Name&gt;' (0x838) has exited with code 0 (0x0).
  The thread 'vshost.LoadReference' (0x1744) has exited with code 0 (0x0).
  'MY_APP.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Primary\PROJECTS\VasoNova\WORK\Win7 VPS\MY_APPG4\bin\Debug\MY_APP.exe', Symbols loaded.
  'MY_APP.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Primary\PROJECTS\VasoNova\WORK\Win7 VPS\MY_APPG4\bin\Debug\VN.Common.dll', Symbols loaded.
  'MY_APP.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Primary\PROJECTS\VasoNova\WORK\Win7 VPS\MY_APPG4\bin\Debug\GuiServer.dll', Symbols loaded.
  'MY_APP.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Primary\PROJECTS\VasoNova\WORK\Win7 VPS\MY_APPG4\bin\Debug\GuiManager.dll', Symbols loaded.
  'MY_APP.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Primary\PROJECTS\VasoNova\WORK\Win7 VPS\MY_APPG4\bin\Debug\VNUserControls.dll', Symbols loaded.
  'MY_APP.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Primary\PROJECTS\VasoNova\WORK\Win7 VPS\MY_APPG4\bin\Debug\VNSockCommunication.dll', Symbols loaded.
  </code></pre>
  
  <p>COMMS STEP 0: MY_APPForm_Load. MY APP NOW CALLS A C FUNCTION IN THE DLL, WHICH CAUSES THIS ERROR:</p>
  
  <pre><code>A first chance exception of type 'System.BadImageFormatException' occurred in MY_APP.exe
  </code></pre>