Why is my application, which I compiled with Any CPU, running as a 32-bit process on my 64-bit machine?

5.3k views Asked by At

Why is my application, which I compiled with AnyCPU, running as a 32-bit process on my 64-bit machine, and therefore unable to interact with Notepad, which is running as a 64-bit process?

I have the following code which will not run on x64 Operating System since notepad.exe is x64 and a x86 application cannot get the modules information of a x64 process:

prc.Modules[0].FileName

.Net Exception throws on my code:

System.ComponentModel.Win32Exception (0x80004005): A 32 bit processes cannot access modules of a 64 bit process.

According to many answers and articles on this forum, MSDN, ..., I know that I need to use AnyCPU instead because of the fact that using x64 has no specific benefit for me. Even when Compile on AnyCPU configuration, my error persists, furthermore, in Task Manager I see a (32-bit) at the end of my process name.

(Actually I tested the code with checking performance and the x64 code ran ~40 ms faster. never mind I do not want my code run 40 ms faster :D )

I do not know that is wrong.

VS 2011 Beta (x64)

Windows 8 Consumer Preview (x64)

Sincerely yours, Peyman Mortazavi

2

There are 2 answers

4
jglouie On BEST ANSWER

Go into the project Properties. On the left side, select Build.

Take a look at the "Platform target:" setting for the current active platform. Change it to x64.

(I suspect your "Platform target:" will be explicitly x86 for "Any CPU")

enter image description here

2
dhiraj suvarna On

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://learn.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:

Executables compiled with /platform:anycpu32bitpreferred execute on the 32-bit CLR.

Here is your culprit.

enter image description here