为什么我的应用程序(我用Any CPU编译)在我的64位机器上作为32位进程运行?

Pey*_*man 6 c# 64-bit anycpu

我有以下代码,不能在x64操作系统上运行,因为notepad.exe是x64,x86应用程序无法获取x64进程的模块信息.

prc.Modules[0].FileName
Run Code Online (Sandbox Code Playgroud)

.Net异常会抛出我的代码

System.ComponentModel.Win32Exception(0x80004005):32位进程无法获得64位进程的模块.

根据这个论坛上的许多答案和文章,MSDN,...,我知道我需要使用AnyCPU,因为使用x64对我没有特别的好处.(不好意思:我测试了一个检查性能的代码,x64代码跑得快了~40毫秒.没关系我不希望我的代码运行速度快40毫秒:D)相反我不会有这样的问题.但即使在AnyCPU配置上编译时也是如此.我的错误仍然存​​在,此外,在任务管理器中,我在进程名称的末尾看到一个(32位).

我不知道那是错的.VS 2011 Beta(x64)Windows 8 Consumer Preview(x64)

真诚的,Peyman Mortazavi

dhi*_*rna 8

Though the questioner has accepted the answer, I feel like the answer is incomplete since the questioner has mentioned that he is using Visual Studio 2011 and hence assuming the target .Net Framework would be 4.5 there are few caveats with respect to what "AnyCPU" means.

Please refer to both these links, to get a better understanding of how the meaning of "AnyCPU" has changed over time.

  1. http://blogs.microsoft.co.il/sasha/2012/04/04/what-anycpu-really-means-as-of-net-45-and-visual-studio-11/
  2. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/platform-compiler-option

From these links, you can arrive at the answer to the question of why your application is running as 32 bit process

On a 64-bit Windows operating system:

使用 /platform:anycpu32bitpreferred 编译的可执行文件在 32 位 CLR 上执行。

这是你的罪魁祸首。

在此处输入图片说明

  • 这是正确的答案。“任何 CPU”都是必要的,但还不够。必须取消选中“首选 32 位”! (2认同)

jgl*_*uie 2

进入项目属性。在左侧,选择构建。

查看当前活动平台的“平台目标:”设置。将其更改为 x64。

(我怀疑您的“平台目标:”对于“任何 CPU”将明确为 x86)

在此输入图像描述