同时使用X86和任何CPU的C#项目 [英] C# projects using both X86 and Any CPU

查看:203
本文介绍了同时使用X86和任何CPU的C#项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有2个Winform项目A,B.

Say I have 2 Winform projects A, B.

项目A(目标.NET 2.0)必须在x86上运行(它是一个外部库),并且由于遗留原因,项目B(目标.NET 4.0)必须在任何CPU上运行.现在我需要从项目B调用A的方法,但是会引发错误:

Project A (target .NET 2.0) has to be run on x86 (it is an external library) and for legacy reasons project B (target .NET 4.0) has to be run on Any CPU. Now I need to call A's methods from project B but it throws an error:

"An unhandled exception of type 'System.BadImageFormatException' occurred in System.Windows.Forms.dll"

Additional information: Could not load file or assembly 'CSharpDemo, Version=1.0.5414.18636, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format."

但是,我发现如果我将.NET 4.5上的项目B作为目标,则不会发生此问题.但是我仍然希望Project B的目标是4.0,如何实现呢?

However, I found if I target project B on .NET 4.5 then this problem doesn't occur. But I still want Project B to be targeting 4.0, how to achieve this?

推荐答案

如果您的任何代码调用32位x86库,则整个过程都需要作为x86加载.没有例外.

If any of your code calls 32-bit x86 libraries, the entire process needs to load as x86. No exceptions.

为此,请将EXE项目设置为x86,将具有本机x86依赖项的DLL项目设置为x86,对于纯托管DLL项目,您可以使用AnyCPU-.NET将它们作为x86加载为x86 EXE,但是它们还可以加载到x64 EXE中.

To do this, set the EXE project to x86, set DLL projects that have native x86 dependencies to x86, and for pure managed DLL projects you can use AnyCPU -- .NET will load them as x86 for your x86 EXE, but they can also load into x64 EXEs.

请记住,借助WOW64兼容性层,x86代码可以在运行x64 Windows的x64处理器上正常运行.

Remember that x86 code runs just fine on x64 processors running x64 Windows, with the help of the WOW64 compatibility layer.

(有限的例外:在Windows Server版本上,WOW64是可选组件,在预安装环境中没有WOW64).

(with limited exceptions: on Windows Server edition, WOW64 is an optional component, and in the preinstall environment there is no WOW64).

这篇关于同时使用X86和任何CPU的C#项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆