同时使用X86和任何CPU的C#项目

编程入门 行业动态 更新时间:2024-10-28 16:29:57
本文介绍了同时使用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#项目

本文发布于:2023-11-11 13:44:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1578614.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目   CPU

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!