用于 C# 应用程序的 AnyCPU/x86/x64 及其 C++/CLI 依赖项 [英] AnyCPU/x86/x64 for C# application and it's C++/CLI dependency

查看:70
本文介绍了用于 C# 应用程序的 AnyCPU/x86/x64 及其 C++/CLI 依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Windows 开发人员,我使用的是 Microsoft Visual Studio 2008 SP1.我的开发者机器是 64 位.

I'm Windows developer, I'm using Microsoft visual studio 2008 SP1. My developer machine is 64 bit.

我目前正在开发的软件是用 C# 编写的托管 .exe.不幸的是,我无法仅用 C# 解决整个问题.这就是为什么我还在 C++/CLI 中开发了一个小的托管 DLL.两个项目都在同一个解决方案中.

The software I'm currently working on is managed .exe written in C#. Unfortunately, I was unable to solve the whole problem solely in C#. That's why I also developed a small managed DLL in C++/CLI. Both projects are in the same solution.

我的 C# .exe 构建目标是任何 CPU".当我的 C++ DLL 构建目标是x86"时,未加载 DLL.据我在谷歌搜索时了解到,原因是 C++/CLI 语言与其他 .NET 语言不同,编译为本机代码,而不是托管代码.

My C# .exe build target is "Any CPU". When my C++ DLL build target is "x86", the DLL is not loaded. As far as I understood when I googled, the reason is C++/CLI language, unlike other .NET languages, compiles to the native code, not managed code.

我将 C++ DLL 构建目标切换到 x64,现在一切正常.但是,一旦我的客户将我的产品安装在 32 位操作系统上,AFAIK 一切都会停止工作.我必须支持 Windows Vista 和 7,它们分别是 32 位和 64 位版本.

I switched the C++ DLL build target to x64, and everything works now. However, AFAIK everything will stop working as soon as my client will install my product on a 32-bit OS. I have to support Windows Vista and 7, both 32 and 64 bit versions of each of them.

我不想退回到 32 位.我的 DLL 中的 250 行 C++ 代码仅占我代码库的 2%.而且那个DLL只用在几个地方,所以在典型的使用场景中它甚至没有加载.

I don't want to fall back to 32 bits. That 250 lines of C++ code in my DLL is only 2% of my codebase. And that DLL is only used in several places, so in the typical usage scenario it's not even loaded.

我的 DLL 用 ATL 实现了两个 COM 对象,所以我不能使用 "/clr:safe" 项目设置.

My DLL implements two COM objects with ATL, so I can't use "/clr:safe" project setting.

有没有办法配置解决方案和项目,以便 C# 项目构建任何 CPU"版本,C++ 项目构建 32 位和 64 位版本,然后在运行时托管.EXE 正在启动,它使用 32 位 DLL 或 64 位 DLL 取决于操作系统?

Is there way to configure the solution and the projects so that C# project builds "Any CPU" version, the C++ project builds both 32 bit and 64 bit versions, then in the runtime when the managed .EXE is starting up, it uses either 32-bit DLL or 64-bit DLL depending on the OS?

或者也许有一些我不知道的更好的解决方案?

Or maybe there's some better solution I'm not aware of?

提前致谢!

推荐答案

没有简单的方法可以解决.如果您有本机代码(即您的 C++)并且您需要支持 x86,那么您必须编译 x86(除非您想在 WOW 世界中工作......即在 32 位和 64 位环境中运行 32 位代码).您可以同时拥有 x86 和 x64 发行版,但是如果您同时支持 32 位和 64 位,并且您拥有本机代码或 COM introp',那么您必须拥有32 位和 64 位二进制文​​件.任何 CPU"只有在没有本机代码或互操作时才真正有用,然后你就会得到好处.

There is no easy way around it. If you have native code (i.e. your C++) and you need to support x86, then you have to compile x86 (unless you want to work in WOW world...ie. running 32 bit code in both 32 and 64 bit envrionments). You can have both an x86 and x64 distributions, but if you're supporting both 32 and 64 bit, and you have native code or COM introp' then you have to have both 32 and 64 bit binaries. "Any CPU" only really is useful when there is no native code or interop, then you get that benifit.

这篇关于用于 C# 应用程序的 AnyCPU/x86/x64 及其 C++/CLI 依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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