无法加载文件或程序集“B.DLL”或其依赖项之一
同时:尝试加载格式不正确的程序
我有引用 B.DLL 的 A.EXE。两个都是我的。两者的平台目标均设置为 x86。 B.DLL参考:
System
System.Data
System.Windows.Forms
System.XML
我在Win7 64位上使用VS2010。我能找到的所有搜索结果都表明这是因为将 32 位程序集加载到 64 位可执行文件中,反之亦然。不过,我已经检查并重新检查了 A.EXE 和 B.DLL 是否设置为使用“x86”作为平台目标。我还尝试过清理和重建整个解决方案。 B.DLL 构建时没有错误或警告。
这是之前的一个工作项目。我目前正在通过将一些代码从 A.EXE 移出到新创建的 B.DLL 中来重构它。
我尝试将 A.EXE 设置为“任何 CPU”,但没有帮助(我没想到会如此)。将其改回“x86”也没有帮助。
我的目标是 .NET 3.5
更新
我创建了一个新的 C# 类库 C.DLL 并添加了对 B.DLL 的引用。 C.DLL 设置为 x86 并且可以正常编译。
更新
MainForm.resx 中的“数据”结束标记处发生编译时错误:
<data name="StatusStripImages.ImageStream"
mimetype="application/x-microsoft.net.object.binary.base64">
<value>
...
</value>
</data>
更新
我现在怀疑 resgen.exe。我尝试强制它为 32 位,但我破坏了它(请参阅:http://social.msdn.microsoft.com/Forums/en-US/vseditorprerelease/thread/6782c692-a9b6-4930-a099-4ee4092e91a9)。
有什么想法吗?
谢谢,安迪
Along with: An attempt was made to load a program with an incorrect format
I have A.EXE that references B.DLL. Both are mine. Both are set to x86 for the platform target. B.DLL references:
System
System.Data
System.Windows.Forms
System.XML
I am using VS2010 on Win7 64-bit. All search results I can find say that this is because of loading 32-bit assemblies into 64-bit executables or vice versa. However I have checked and rechecked that A.EXE and B.DLL are set to use "x86" as the platform target. I've also tried cleaning and rebuilding the entire solution. B.DLL builds with no errors or warnings.
This is a previously working project. I'm currently working on refactoring it by moving some code out of A.EXE an into the newly-created B.DLL.
I tried setting A.EXE to "Any CPU" and it didn't help (I didn't expect it to). Changing it back to "x86" didn't help either.
I am targeting .NET 3.5
Update
I created a new C# class library C.DLL and added a reference to B.DLL. C.DLL is set to x86 and it compiles fine.
Update
The compile-time error occurs in MainForm.resx at a closing "data" tag:
<data name="StatusStripImages.ImageStream"
mimetype="application/x-microsoft.net.object.binary.base64">
<value>
...
</value>
</data>
Update
I now suspect resgen.exe. I tried forcing it to be 32-bit and I broke it (see: http://social.msdn.microsoft.com/Forums/en-US/vseditorprerelease/thread/6782c692-a9b6-4930-a099-4ee4092e91a9).
Any ideas?
thanks, Andy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过对两个程序集运行“corflags”来验证它们是否正确设置了 32 位标志。
You can verify if the 32bit flag is correctly set on both assemblies by running "corflags" against them.
通常,当尝试在 x64 应用程序中加载 x86 构建的 dll 时,会发生 BadFormatException 异常。
在项目属性中,重新检查平台目标是否设置为 x86 或 x64。混合两者会产生问题。
Generally the exception BadFormatException occurs when a dll is built in x86 is tried to be loaded in x64 application.
In your project properties Recheck if the Platform Target is set both either to x86 or to x64. Mixing up both creates an issue.
这里没有太多的故障模式。但请记住,调试版本和发布版本的目标平台设置是分开的。而且VS2010中的配置名称实际上与目标平台设置没有任何关系。换句话说,您可以更改目标平台,但它在 Build + Configuration Manager 中仍将被命名为“x86”。棘手。
因此,请仔细检查您的假设。实际验证两种配置的目标平台组合。 Bob 建议使用 Corflags.exe 进行三次检查是个好主意。从 Visual Studio 命令提示符运行它。只有 EXE 设置很重要,DLL 必须遵循启动程序集的要求。如果您的代码通过 EXE 之外的其他方式启动,则会出现其他故障模式。就像单元测试运行者一样。
There are not a lot of a failure modes here. But do keep in mind that the Target Platform setting is separate for the Debug and the Release version. And that the configuration name in VS2010 doesn't actually have anything to do with the Target Platform setting. In other words, you can change the Target Platform but it will still be named "x86" in Build + Configuration Manager. Tricky.
So double-check your assumptions. Actually verify the Target Platform combo for both configurations. And Bob's recommendation to triple-check with Corflags.exe is a good idea. Run it from the Visual Studio Command prompt. Only the EXE setting matters, the DLLs have to follow suit with what the startup assembly demanded. There are additional failure modes if your code gets started by other means than a EXE. Like a unit-test runner.
修复
我遇到了该问题当前接受的答案所描述的确切问题,该答案指向下面的链接
http://blogs.msdn.com/b/ Visualstudio/archive/2010/06/19/resgen-exe-error-an-attempt-was-made-to-load-a-program-with-an-in Correct-format.aspx
但是,在阅读此内容之后:
https://connect.microsoft.com/VisualStudio/feedback/details/532584/error-when-compiling-resx-file-seems-lated-to-beta2-bug-5252020
看来,如果您打开失败的 .resx 文件,您将看到类似于
此元素内有一个数据块的行。第一行显示
只需将最后一个 0 替换为 y ,使该行显示
为这立即对我有用。我喜欢它的拼写方式j0y。这不是一个很好的修复,但这是我见过的最好的修复。
j0y,
----纳努克
FIX
I was suffering from the exact issue described by the current accepted answer for this question that points to the link below
http://blogs.msdn.com/b/visualstudio/archive/2010/06/19/resgen-exe-error-an-attempt-was-made-to-load-a-program-with-an-incorrect-format.aspx
However, after eading this:
https://connect.microsoft.com/VisualStudio/feedback/details/532584/error-when-compiling-resx-file-seems-related-to-beta2-bug-5252020
It appears that if you open the failing .resx file you will a line similar to
There is a block of data within this element. The first line reads
Simply replace last 0 with a y making the line read
This worked for me straight away. I like how it spells out j0y. Not a great fix but it's the best I've seen short of a fix.
j0y,
----Nanook
我似乎遇到了 Visual Studio 2010 中的一个错误 [1],尽管 Microsoft 几个月前就知道这个错误,但 Microsoft 却懒得修复。
有三种解决方法,我只是将 A.EXE 和 B.DLL 从 .NET 3.5 更改为 .NET 4.0 来解决它。不理想,但我不想对 resgen.exe 进行更改。我想我必须等待 SP1 才能针对 .NET 3.5。
[1] http://blogs.msdn.com/b/visualstudio/archive/2010/06/19/resgen-exe-error-an-attempt -被制作为加载带有不正确格式的程序.aspx
It seems I am suffering from a bug [1] in Visual Studio 2010 that Microsoft can't be bothered to fix even though they've known about it for months.
There are three workarounds and I just changed A.EXE and B.DLL from .NET 3.5 to .NET 4.0 to solve it. Not ideal but I didn't want to make changes to resgen.exe. I guess I have to wait for SP1 to be able to target .NET 3.5.
[1] http://blogs.msdn.com/b/visualstudio/archive/2010/06/19/resgen-exe-error-an-attempt-was-made-to-load-a-program-with-an-incorrect-format.aspx