何时在 Visual Studio 中以 x64 为目标

发布于 2024-12-20 09:42:21 字数 869 浏览 0 评论 0原文

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

再进一步看,我发现这篇有用的文章并且这篇文章帮助解释了事情。

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

因此,如果我在 x64 系统上,我可以指定“任何 CPU”以本机运行,但不支持 32 位 dll;我可以指定“x86”,它将支持 32 位 dll(因为它们都将在 WOW64 下运行); 但是我什么时候会指定“x64”?看来 x64 系统上的“任何 CPU”场景将支持 64 位 dll。如果我想阻止某人在 32 位系统上运行我的应用程序或确保在尝试加载 32 位 dll 时失败,是否可以这样做?

在我看来,如果您的项目中有一些第三方 dll 需要担心,您需要将其设置为“任何 CPU”之外的其他值。对于不处理其他 dll 的每个其他项目,最好将其保留为“任何 CPU”吗?

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

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.

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

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).

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?

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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人如玉 2024-12-27 09:42:21

是的,如果您调用本身是 64 位的 DLL(因为它是本机的,或者是本身调用 64 位本机 DLL 的托管 DLL,等等),您将指定项目应编译为 x64。

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

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

即使您正在处理本机 dll,如果您使用的是 PInvoke,您仍然可以将其保留为“any”;您可以有两个版本的类来包装它,一种用于 x86,一种用于 x64,并通过检查 IntPtr.Size 属性来选择在运行时使用哪一种。

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

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).

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.

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.

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.

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.

绾颜 2024-12-27 09:42:21

如果您通过 COM 或 P/Invoke 使用没有 32 位版本的本机代码,则需要指定 x64。

You would specify x64 if you're using native code through COM or P/Invoke that doesn't have a 32-bit version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文