System.BadImageFormatException 尝试加载格式不正确的程序
我正在为另一个基于公共 .NET API 的程序编写插件。通常,这些插件是通过创建引用 API 程序集的类库 DLL 来制作的。然后,通过继承 API 程序集中的基命令类来创建命令类。然后,应用程序被设置为引用插件 DLL 文件,并且还负责在用户请求时实际启动自定义命令类。
但是,现在我尝试通过 System.CodeDOM 自动生成一些代码,并希望创建一个简单的控制台应用程序,该应用程序根据 API 程序集中的类型自动生成新的类类型。
然而,当我尝试运行我的应用程序时,出现以下异常。
System.BadImageFormatException 是 未处理的消息:无法加载文件 或程序集'RevitAPI, 版本=2011.0.0.0,文化=中立, PublicKeyToken=null' 或其之一 依赖关系。有人尝试 加载一个不正确的程序 格式。
通常我需要将插件程序集的目标框架设置为3.5。但现在我发现,如果我将控制台应用程序的目标框架设置为 2.0,上述错误就会消失。但是,我的控制台应用程序已经引用了我的其他类库,它们的目标框架设置为 3.5。我真的不想围绕 2.0 框架重写它们。
I'm writing a plug-in for another program that is based on a public .NET API. Typically these plugins are made by creating a class library DLL that references the API assembly. Then a command class is created by inheriting from a base command class in the API assembly. The application is then set to reference the plug-in DLL file, and is then also responsible for actually firing up the custom command class when the user requests it.
However, now I'm trying to automate some code generation through System.CodeDOM
, and want to create a simple console application that automatically generates new Class Types based off of types with in the API assembly.
Yet, when I try to run my application I get the following exception.
System.BadImageFormatException was
unhandled Message: Could not load file
or assembly 'RevitAPI,
Version=2011.0.0.0, Culture=neutral,
PublicKeyToken=null' or one of its
dependencies. An attempt was made to
load a program with an incorrect
format.
Usually I need to set the target framework of a plug-in assembly to 3.5. Yet now I've found that the error above goes away if I set the target framework of my console application to 2.0. However, my console application already references other class libraries of mine that have their target framework set to 3.5. And I'd really rather not rewrite them around the 2.0 framework.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(9)
当我的软件在另一台机器上运行时,我遇到了同样的问题。在我的开发人员电脑(Windows 7)上,我安装了 Visual Studio 2015,目标电脑是 Windows 10 的全新安装(安装了 .Net)。我还在另一台干净的 Windows 7 电脑(包括 .Net Framework)上测试了它。但是,在两台目标电脑上,我需要安装 Visual C++ Redistributable适用于 x86 或 x64 的 Visual Studio 2015 包(取决于您的应用程序的构建目的)。它已经安装在我的开发人员电脑上。
我的应用程序使用 C 库,该库已使用 Visual Studio 中的 /clr 和 /TP 选项编译为 C++ 应用程序。该应用程序还通过使用 dllexport 方法签名向 C# 提供函数。不确定 C# 集成是否会导致出现该错误,或者 C++ 应用程序是否会出现相同的错误。
希望它对任何人都有帮助。
另请注意,正在使用的引用 dll 文件的版本非常重要。当我尝试在 MMC 管理单元中使用“HTMLView”或“FormView”类型的 WebView2 组件时,我遇到了与“Microsoft.WebView2.FixedVersionRuntime.101.0.1210.39.x64”几乎相同的问题。
我只是将 dll 文件(在我的例子中为 WebView2Loader.dll,版本 1.0.1248.0,大小=157640 字节)复制到项目可访问的正确路径中(您可以先将其放在项目输出文件旁边来测试它)然后 WebView2 浏览器开始按预期运行。微软的错误消息有时(至少在我的情况下)有点误导性,并且没有传达足够的、中肯的信息。
我收到“BadImageFormatException”,通常在您混合平台目标(例如,在针对 x86 的应用程序中使用以 X64 编译的 dll 文件,反之亦然)或混合本机代码和 .NET 时发生,但这根本不是我的问题。我希望这能帮助那些可能陷入困境的人。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
这可能是 32 - 64 位不匹配。
如果您在 64 位操作系统上运行,则 Assembly RevitAPI 可能会编译为 32 位,而您的进程可能会编译为 64 位或“任何 CPU”。
或者,RevitAPI 编译为 64 位,而您的进程编译为 32 位或“任何 CPU”并在 32 位操作系统上运行。
It's possibly a 32 - 64 bits mismatch.
If you're running on a 64-bit OS, the Assembly RevitAPI may be compiled as 32-bit and your process as 64-bit or "Any CPU".
Or, the RevitAPI is compiled as 64-bit and your process is compiled as 32-bit or "Any CPU" and running on a 32-bit OS.