.NET 软件在 x64 上运行,但在 x86 上编译用于任何 CPU (VS2008)
我有一个问题,编译的程序出现运行时错误。这在开发环境(x86)中运行良好,但在生产环境(x64)中运行不佳。
我找到了这篇文章,这似乎意味着,如果在 64 位环境中运行,则将软件编译为任何 CPU 都可以将软件作为 x64 软件运行。
是这样吗?如果是这样,软件是否应该在任何 CPU 下编译(因为这似乎会导致两个环境之间不匹配)?
I have a problem, whereby I’m getting runtime errors with a compiled program. This works fine in the development environment (x86), but not on the production environment (which is x64).
I found this article, which seems to imply that compiling the software as Any CPU may run the software as x64 software if it’s run in a 64 bit environment.
Is this the case, and if so, should software ever be compiled under Any CPU (as it seems to make for a mismatch between the two environments)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您仅使用托管代码,AnyCPU 就非常有用。托管代码可以在 32 位进程或 64 位进程中执行。对于 AnyCPU,框架选择操作系统本机的位数。
对于非托管代码来说情况并非如此。它需要编译为 32 位或 64 位代码。当然,由于WoW64子系统,64位Windows可以运行64位进程。
但是 64 位进程无法加载 32 位模块(DLL、OCX 等)。
这意味着如果您的程序使用某种类型的非托管模块,最好确保手动设置位数,以便所有组件都具有相同的位数。
AnyCPU is great if you are using managed code only. Managed code can be executed in a 32-bit-process or 64-bit-process. With AnyCPU, the framework chooses the bitness native to the OS.
The same is not true for unmanaged code. It needs to be compiled either as 32-bit or 64-bit code. Of course, due to the WoW64 subsystem, 64-bit Windows can run 64-bit processes.
But 64-bit processes cannot load 32-bit modules (DLLs, OCXs, etc.).
That means if your program is using unmanaged modules of some kind, better make sure to set the bitness manually so that all components have the same bitness.