System.BadImageFormatException 尝试加载格式不正确的程序

发布于 2024-10-06 05:58:23 字数 583 浏览 14 评论 0原文

我正在为另一个基于公共 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

我是男神闪亮亮 2024-10-13 05:58:23

这可能是 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.

浅听莫相离 2024-10-13 05:58:23

如果您使用 IIS,
进入应用程序池
选择您的网站使用的选项,然后单击高级设置
确保启用 32 位应用程序设置为 True

If you use IIS,
Go to the Application pool
Select the one that your site uses and click Advance Settings
Make sure that the Enable 32-Bit Applications is set to True

无风消散 2024-10-13 05:58:23

我在使用 x64 项目新安装 VS 时遇到问题 - 对于 Visual Studio 2013、Visual Studio 2015 和 Visual Studio 2017:

Tools
  -> Options
   -> Projects and Solutions
    -> Web Projects
     -> Check "Use the 64 bit version of IIS Express for web sites and projects"

I was having problems with a new install of VS with an x64 project - for Visual Studio 2013, Visual Studio 2015 and Visual Studio 2017:

Tools
  -> Options
   -> Projects and Solutions
    -> Web Projects
     -> Check "Use the 64 bit version of IIS Express for web sites and projects"
疧_╮線 2024-10-13 05:58:23

这些建议是准确的,但我想补充一点。我陷入困境只是因为我有多个发布配置。我正在编辑“调试 - 任何 CPU”,然后部署“调试 - x64”配置。确保您正在编辑和部署相同的配置。开始发布并弹出“发布 Web”对话框后,请单击“设置”选项卡来验证这一点。确保它与您编辑的配置匹配。 (那是我生命中的 4 个小时,我永远不会再回来了!)

These suggestions are accurate, but I wanted to add a note. I was stuck simply because I had multiple publishing configurations. I was editing the "Debug - Any CPU" and then deploying the "Debug - x64" configuration. Make sure you are editing and deploying the same configuration. Verify this by clicking the "Settings" tab after you begin publishing and the "Publish Web" dialog pops up. Make sure it matches the configuration you edited. (That's 4 hours of my life I will never get back!)

分開簡單 2024-10-13 05:58:23

当我的软件在另一台机器上运行时,我遇到了同样的问题。在我的开发人员电脑(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++ 应用程序是否会出现相同的错误。

希望它对任何人都有帮助。

I had the same issue when getting my software running on another machine. On my developer pc (Windows 7), I had Visual Studio 2015 installed, the target pc was a clean installation of Windows 10 (.Net installed). I also tested it on another clean Windows 7 pc including .Net Framework. However, on both target pc's I needed to install the Visual C++ Redistributable for Visual Studio 2015 package for x86 or x64 (depends on what your application is build for). That was already installed on my developer pc.

My application was using a C library, which has been compiled to a C++ application using /clr and /TP options in visual studio. Also the application was providing functions to C# by using dllexport method signatures. Not sure if the C# integration leaded to give me that error or if a C++ application would have given me the same.

Hope it helps anybody.

罗罗贝儿 2024-10-13 05:58:23

要在任何 62 位或 32 位 CPU 上运行它,请按照以下步骤操作:
在解决方案资源管理器中右键单击项目名称>属性>构建
并将它们放在配置:活动(发布)、平台:活动(任何 CPU) 和目标:x86 下。
在运行按钮旁边,从选项中选择选项释放任何CPU
然后保存并运行。

For running it on any CPU either 62 bit or 32 bit follow these steps:
Right click on the name of the project in Solution Explorer> Properties>Build
and have these under Configuration: Active(Release), Platform:Active(Any CPU) and Target:x86.
and just beside the Run button Select option Release and Any CPU from the options.
And then Save it and Run.

暮光沉寂 2024-10-13 05:58:23

输入图片这里的描述

我试图加载具有x86架构的dll。在 C# 项目中,我将平台目标描述为 x86 并消除了错误

enter image description here

i was trying to load dll that had x86 architecture . in c# project i described platform target as x86 and got rid of the error

嗳卜坏 2024-10-13 05:58:23

我有同样的问题,我刚刚下载了 32 位 dll 并将其添加到我的 bin 文件夹中,这解决了我的问题

i have same problem what i did i just downloaded 32-bit dll and added it to my bin folder this is solved my problem

一袭水袖舞倾城 2024-10-13 05:58:23

另请注意,正在使用的引用 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 时发生,但这根本不是我的问题。我希望这能帮助那些可能陷入困境的人。

Please also note that the version of referenced dll file(s) which is in use is very crucial. I had almost the same problem with "Microsoft.WebView2.FixedVersionRuntime.101.0.1210.39.x64" when I tried to use the WebView2 component in the MMC Snap-Ins with types of "HTMLView" or "FormView".

I just copied the dll file (in my case WebView2Loader.dll, version 1.0.1248.0, size=157640 bytes) in a proper path that was accessible for the project (you could just put it beside your project output files first to test it) and then WebView2 browser started to function as expected. Microsoft error messages sometimes (at least in my case) was a little bit misleading and did not convey enough and to the point information.

I received "BadImageFormatException" that normally occurs when you mix platform targets (for example using a dll file compiled in X64 in an application that targeted for x86 or vice versa) or mix native code and .NET but that was not my problem at all. I hope this help one who may stuck in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文