何时在 Visual Studio 中面向 x64 [英] When to target x64 in Visual Studio

查看:23
本文介绍了何时在 Visual Studio 中面向 x64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始了一份新工作,我注意到每个人都在谈论的第一件事是更新"所有我们的 .NET 应用程序到 x64.我最初认为这很奇怪,因为我们都知道 .NET 编译为与平台无关的 IL,并且特定的 CLR 运行代码.

I recently started a new job, and one of the first things I noticed everybody talking about was "updating" All our .NET apps to x64. I initially thought this odd since we all know .NET compiles to platform agnostic IL and the specific CLR runs the code.

再进一步看,我发现了这篇有用的文章this SO post 有助于解释事情.

Looking a bit further, I found this helpful article and this SO post which helped explain things.

所以现在我明白 IL 没有改变,只有元数据基本上说在 WOW64 或不在 x64 系统上运行(简而言之).

So now I understand that the IL isn't changed, only meta data basically saying to run in WOW64 or not on a x64 system (in a nutshell).

所以如果我在 x64 系统上,我可以指定任何 CPU"来本地运行,但不支持 32 位 dll;我可以指定支持 32 位 dll 的x86"(因为它们都将在 WOW64 下运行);但我什么时候指定x64"?在 x64 系统上的任何 CPU"方案中似乎支持 64 位 dll.这是如果我想阻止某人在 32 位系统上运行我的应用程序或确保在尝试加载 32 位 dll 时失败?

So if I'm on a x64 system, I can specify "Any CPU" to run natively, but won't support 32bit dlls; I can specify "x86" which will support 32bit dlls (since they both would be running under WOW64); but when would I specify "x64"? It seems that 64bit dlls would be supported in the "Any CPU" scenario on a x64 system. Is this if I want to prevent someone from running my app on a 32bit system or ensuring failure when trying to load 32bit dlls?

在我看来,如果您的项目中需要担心某些 3rd 方 dll,则您需要将其设置为任何 CPU"以外的其他内容.对于不处理其他 dll 的每个其他项目,最好将其保留为任何 CPU"吗?

It would also seem to me that you would only need to set it to something other than "Any CPU" if you have some 3rd party dll to worry about in your project. Is it best to leave it as "Any CPU" for every other project not dealing with others dlls?

如果我碰巧将目标设置为x86",因为我有一个 32 位的 3rd 方 dll,如果在 WOW64 下的 64 位系统上,我的应用程序实际上是否被认为是在 64 位上运行的?

If I do happen to set my target to "x86" because I have a 32bit 3rd party dll, is my application actually considered to be running in 64bit if on a 64bit system just under WOW64?

推荐答案

是的,如果您正在调用本身为 64 位的 DLL(因为它是本机的,或者是本身调用 64 位本机 DLL 等的托管 DLL.

Yes, you would specify that the project should compile to x64 if you're calling a DLL that is itself 64 bit (either because it is native, or is a managed DLL that's itself calling a 64 bit native DLL, etc).

同样,如果您正在处理 32 位 3rd 方 DLL,则指定它应该是 x86;如果在 64 位版本的 Windows 上运行,它将不被视为 64 位应用程序.

Likewise for specifying that it should be x86 if you're dealing with 32-bit 3rd party DLLs; it will not be considered a 64-bit application if running on a 64-bit version of Windows.

如果您只是处理纯托管代码,那么我会将其保留为任何".我通常也将 DLL 保留为任何",即使可执行文件将被指定为 x86 或 x64.

If you're just dealing with pure managed code, then I'd leave things as "any". I also generally leave DLLs as "any" as well, even if the executeable will be specified to be either x86 or x64.

即使您正在处理本机 dll,如果您使用 PInvoke,您仍然可以将其保留为任何";您可以有两个版本的类环绕它,一个用于 x86,一个用于 x64,然后通过检查 IntPtr.Size 属性选择在运行时使用哪个版本.

And even if you're dealing with native dlls, you might still be able to get away with leaving it as "any" if you're using PInvoke; you can have two versions of a class that wraps around it, one for x86, one for x64, and choose which one to use at runtime by checking the IntPtr.Size property.

当然,如果您的应用程序需要超过 4 GB 的 RAM,并且您想强制它必须在 64 位操作系统上运行,那么您还需要以 x64 为目标.

And of course, if you're application needs more than 4 GBs of RAM and you want to enforce that it has to run on a 64-bit OS, then you'll also need to target x64.

这篇关于何时在 Visual Studio 中面向 x64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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