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

2

11

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

Kiruna answered 6/6, 2012 at 16:35 Comment(3)
Are you SURE you compiled with Any CPU when you got that exception?Lug
The configuration name has nothing to do with the Platform target setting. Awkward problem on projects that were started on earlier versions of VS.Ramsden
Dear Hans, I created this project on Visual Studio 2011.Kiruna
S
2

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

Swee answered 6/6, 2012 at 16:43 Comment(4)
Also check the project configuration manager. Sometimes it will show which project has been setup incorrectly.Renata
I've checked it. All of them are correct. I think the problem is Visual Studio 2011. because I remember I had no problem with this code in the past.Kiruna
Why changing to x64? to make sure the problem's cause is the compile configuration? I've changed that to x64 and it worked without throwing that Exception. so the problem is exactly the compiler configuration.Kiruna
Yes, to make sure or to serve as a workaround in the event that vs2011 is the problem.Swee
S
16

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

Stridulous answered 10/12, 2017 at 13:10 Comment(2)
This is the right answer. Any CPU is necessary but is not sufficient. Prefer 32-bit have to be unchecked!Towe
I would also like to add that you should check <RuntimeIdentifier> property in .NET SDK style projects. For instance, if all above properties are checked but you have win7-x86 runtime identifier it will still be run as a 32-bit process.Brader
S
2

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

Swee answered 6/6, 2012 at 16:43 Comment(4)
Also check the project configuration manager. Sometimes it will show which project has been setup incorrectly.Renata
I've checked it. All of them are correct. I think the problem is Visual Studio 2011. because I remember I had no problem with this code in the past.Kiruna
Why changing to x64? to make sure the problem's cause is the compile configuration? I've changed that to x64 and it worked without throwing that Exception. so the problem is exactly the compiler configuration.Kiruna
Yes, to make sure or to serve as a workaround in the event that vs2011 is the problem.Swee

© 2022 - 2024 — McMap. All rights reserved.