【问题标题】:Why is my .NET app trying to load unrelated DLL files? (First chance System.BadImageFormatException in debug output)为什么我的 .NET 应用程序试图加载不相关的 DLL 文件? (调试输出中的第一次机会 System.BadImageFormatException)
【发布时间】:2011-02-10 15:00:55
【问题描述】:

在调试时,我注意到这个 C# 应用程序有以下几点:

它似乎试图加载恰好与可执行文件位于同一目录中的 所有 DLL 文件。 (即使是与此项目/解决方案中的任何内容完全无关的内容。)

应用程序正在加载并且工作正常,但是我发现调试输出很奇怪:(路径被剪断)

...
A first chance exception of type 'System.BadImageFormatException' occurred in mscorlib.dll
'my_test.exe': Loaded '....\release\mfc42u.dll', Symbols loaded (source information stripped).
'my_test.exe': Unloaded '....\release\mfc42u.dll'
A first chance exception of type 'System.BadImageFormatException' occurred in mscorlib.dll
'my_test.exe': Loaded '....\release\mpiwin32.dll', Binary was not built with debug information.
'my_test.exe': Unloaded '....\release\mpiwin32.dll'
A first chance exception of type 'System.BadImageFormatException' occurred in mscorlib.dll
...

上面的两个 DLL 与 C# 项目或它引用的任何东西完全无关。为什么可执行文件(或 VS 调试器?)试图加载这些 DLL?

【问题讨论】:

  • 为什么你的可执行文件目录中首先有那些不需要的 DLL?
  • @DOK - 为什么不呢!?这很正常。如果一个应用程序包含多个可执行文件(并且是 DLL),它们通常仍驻留在一个目录中。
  • @DOK 所说的(我完全同意)是:如果 2 个 .dll 与您的项目(或它引用的任何内容)无关,为什么它们会在您的可执行文件的 bin 目录中?你是手动复制的吗?如果它们不应该被任何东西加载,那么它们可能根本不应该在那里......
  • @Pwinstein:正如马丁所说,如果您在同一个目录中有多个可执行文件,则可执行文件不引用相同的 dll。这就是他们会在那里的原因。
  • 您能否将 VS 配置为 总是System.BadImageFormatException 上中断并发布堆栈跟踪(在 Debug->Exceptions->Thrown下打勾> 对于这个例外)?

标签: c# .net debugging dll assemblies


【解决方案1】:

看来这个应用毕竟在主动加载这些 DLL!

我在一个我不熟悉的组件中找到了这段代码:

...
foreach (FileInfo file in dirInfo.GetFiles())
{
...
  try
  {
    Assembly ass = Assembly.LoadFrom(file.FullName);
...
  catch (Exception)
  {
    // Ignore all errors caught due to the .NET framework not being able to load an assembly.
    // Not all qualifying files in the specified directories really are valid .NET assemblies.
  }
...

0xA3 关于捕捉第一次机会异常的评论让我走上了正轨!

【讨论】:

  • 哇,那个开发者到底在想什么。他甚至不只是加载所有*.dll 文件,而是所有文件。
  • @CodeInChaos:我剪掉了大部分代码。它“仅”检查所有 .dll 文件。
猜你喜欢
  • 2017-03-24
  • 2023-03-12
  • 2017-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多