C# 在 64 位计算机上 buildint 后出现“'System.BadImageFormatException' 类型的第一次机会异常”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18625394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:47:08  来源:igfitidea点击:

"A first chance exception of type 'System.BadImageFormatException' " after buildint on a 64-bit computer

c#.netcwindowsvisual-c++

提问by Doug Null

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:

我的 Visual Studio 2010 解决方案有 C DLL 和 C# 项目,它们调用 C DLL。解决方案在 32 位 Windows-7 Pro 计算机上构建时运行正常。在 64 位 Windows-7 Pro 上,构建没问题,但是当运行时,C# 调用 DLL 中的函数并得到错误:

A first chance exception of type 'System.BadImageFormatException'

Solution Configuration Manager settings are:

解决方案配置管理器设置为:

<DLL project>   Platform = Win32       (<<< no option to change to "Any CPU")
<C# project>    Platform = "Any CPU"

The output below is:

下面的输出是:

  1. C DLL BUILD OUTPUT

  2. C# BUILD OUTPUT

  3. RUN OUTPUT

  1. C DLL 构建输出

  2. C# 构建输出

  3. 运行输出

C DLL BUILD OUTPUT:

C DLL 构建输出:

1>------ Rebuild All started: Project: Ultrasound_Frame_Grabber, Configuration: Debug Win32 ------
1>  Processing ..\..\..\include\USGFWSDK\include\Usgfw2.idl
1>  Usgfw2.idl
1>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\oaidl.idl
1>  oaidl.idl
1>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\objidl.idl
1>  objidl.idl
1>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\unknwn.idl
1>  unknwn.idl
1>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\wtypes.idl
1>  wtypes.idl
1>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\basetsd.h
1>  basetsd.h
1>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\guiddef.h
1>  guiddef.h
1>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\ocidl.idl
1>  ocidl.idl
1>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\oleidl.idl
1>  oleidl.idl
1>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\servprov.idl
1>  servprov.idl
1>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\urlmon.idl
1>  urlmon.idl
1>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\msxml.idl
1>  msxml.idl
1>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\strmif.idl
1>  strmif.idl
1>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\control.odl
1>  control.odl
1>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\oaidl.acf
1>  oaidl.acf
1>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\ocidl.acf
1>  ocidl.acf
1>  stdafx.cpp
1>  DIBSectn.cpp
1>  sample.cpp
1>  sampleDlg.cpp
1>  Ultrasound_Frame_Grabber.cpp
1>  Generating Code...
1>  Usgfw2_i.c
1>  .NETFramework,Version=v4.0.AssemblyAttributes.cpp
1>     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>strmbasd.lib(mtype.obj) : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specification
1>  sample.vcxproj -> C:\Primary\PROJECTS\VasoNova\WORK\Win7 VPS\Debug\Ultrasound_Frame_Grabber.dll
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

2) C# BUILD OUTPUT:

2) C# 构建输出:

1>------ Skipped Rebuild All: Project: VNMath, Configuration: Debug Win32 ------
1>Project not selected to build for this solution configuration 
2>------ Skipped Rebuild All: Project: GuiManager, Configuration: Debug Any CPU ------
2>Project not selected to build for this solution configuration 
3>------ Rebuild All started: Project: Ultrasound_Frame_Grabber, Configuration: Debug Win32 ------
4>------ Skipped Rebuild All: Project: VNCommon, Configuration: Debug Any CPU ------
4>Project not selected to build for this solution configuration 
5>------ Skipped Rebuild All: Project: AudioUtility, Configuration: Debug Any CPU ------
5>Project not selected to build for this solution configuration 
6>------ Skipped Rebuild All: Project: VNDevices, Configuration: Debug Any CPU ------
6>Project not selected to build for this solution configuration 
7>------ Skipped Rebuild All: Project: SimpleRPC, Configuration: Debug Any CPU ------
7>Project not selected to build for this solution configuration 
8>------ Skipped Rebuild All: Project: VNSockCommunication, Configuration: Debug Any CPU ------
8>Project not selected to build for this solution configuration 
9>------ Skipped Rebuild All: Project: PdfSharp, Configuration: Debug Any CPU ------
9>Project not selected to build for this solution configuration 
10>------ Skipped Rebuild All: Project: VNUserControls, Configuration: Debug Any CPU ------
10>Project not selected to build for this solution configuration 
11>------ Skipped Rebuild All: Project: GuiServer, Configuration: Debug Any CPU ------
11>Project not selected to build for this solution configuration 
3>  Processing ..\..\..\include\USGFWSDK\include\Usgfw2.idl
3>  Usgfw2.idl
3>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\oaidl.idl
3>  oaidl.idl
3>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\objidl.idl
3>  objidl.idl
3>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\unknwn.idl
3>  unknwn.idl
3>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\wtypes.idl
3>  wtypes.idl
3>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\basetsd.h
3>  basetsd.h
3>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\guiddef.h
3>  guiddef.h
3>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\ocidl.idl
3>  ocidl.idl
3>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\oleidl.idl
3>  oleidl.idl
3>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\servprov.idl
3>  servprov.idl
3>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\urlmon.idl
3>  urlmon.idl
3>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\msxml.idl
3>  msxml.idl
3>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\strmif.idl
3>  strmif.idl
3>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\control.odl
3>  control.odl
3>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\oaidl.acf
3>  oaidl.acf
3>  Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\ocidl.acf
3>  ocidl.acf
3>  stdafx.cpp
3>  DIBSectn.cpp
3>  sample.cpp
3>  sampleDlg.cpp
3>  Ultrasound_Frame_Grabber.cpp
3>  Generating Code...
3>  Usgfw2_i.c
3>  .NETFramework,Version=v4.0.AssemblyAttributes.cpp
3>     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>strmbasd.lib(mtype.obj) : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specification
3>  sample.vcxproj -> C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\Debug\Ultrasound_Frame_Grabber.dll
12>------ 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>  MY_APP -> C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\MY_APP\bin\Debug\MY_APP.exe
12>  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\CommsLibrary\Common.Logging.dll
12>  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\CommsLibrary\Common.Logging.Log4Net.dll
12>  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\CommsLibrary\DistributedFileSystem.dll
12>  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\CommsLibrary\ICSharpCode.SharpZipLib.dll
12>  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\CommsLibrary\libMobiledevice.dll
12>  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\CommsLibrary\log4net.config
12>  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\CommsLibrary\log4net.dll
12>  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\CommsLibrary\NetworkCommsDotNet.dll
12>  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\CommsLibrary\protobuf-net.dll
12>  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\CommsLibrary\QuickLZCompressor.dll
12>  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\CommsLibrary\SerializerBase.dll
12>  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\CommsLibrary\SevenZipLZMACompressor.dll
12>  C:\Primary\PROJECTS\my_project\WORK\Win7 VPS\CommsLibrary\SharpZipLibCompressor.dll
12>         13 file(s) copied.
========== Rebuild All: 2 succeeded, 0 failed, 10 skipped ==========

3) RUN OUTPUT:

3)运行输出:

'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.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.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.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 '<No Name>' (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.

COMMS STEP 0: MY_APPForm_Load. MY APP NOW CALLS A C FUNCTION IN THE DLL, WHICH CAUSES THIS ERROR:

通信第 0 步:MY_APPForm_Load。我的应用程序现在在 DLL 中调用 AC 函数,这会导致此错误:

A first chance exception of type 'System.BadImageFormatException' occurred in MY_APP.exe

采纳答案by Hans Passant

Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib...

加载 'C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib...

Your program is running as a 64-bit process. Your DLL contains 32-bit unmanaged code. Kaboom.

您的程序作为 64 位进程运行。您的 DLL 包含 32 位非托管代码。卡布姆。

You'll need to force your EXE to run as a 32-bit process so that DLL can be loaded. Right-click your EXE project in the Solution Explorer window. Properties, Build tab, change the Platform target setting from AnyCPU to x86. On VS2012 and up tick the "Prefer 32-bit" option. Repeat for the Release configuration.

您需要强制您的 EXE 作为 32 位进程运行,以便可以加载 DLL。在解决方案资源管理器窗口中右键单击您的 EXE 项目。属性,构建选项卡,将平台目标设置从 AnyCPU 更改为 x86。在 VS2012 及更高版本上,勾选“首选 32 位”选项。对发布配置重复此操作。