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

StackOverflow https://stackoverflow.com/questions/10918366

  •  13-06-2021
  •  | 
  •  

Pregunta

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 acc ess 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. (INCORRECT: I tested a 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 ) instead I will not problems like this. but 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.

I do not know that is wrong. VS 2011 Beta (x64) Windows 8 Consumer Preview (x64)

Sincerely yours, Peyman Mortazavi

¿Fue útil?

Solución

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

Otros consejos

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:

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

Here is your culprit.

enter image description here

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top