Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

64-bit

anycpu

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

like image 601
Peyman Avatar asked Jun 06 '12 16:06

Peyman


People also ask

Can a 32-bit application launch a 64-bit application?

The 64-bit versions of Windows don't provide support for 16-bit binaries or 32-bit drivers. Programs that depend on 16-bit binaries or 32-bit drivers can't run on the 64-bit versions of Windows unless the program manufacturer provides an update for the program.

How do you know if a process is 32-bit or 64-bit?

Click Start, type system in the search box, and then click System in the Control Panel list. The operating system is displayed as follows: For a 64-bit version operating system: 64-bit Operating System appears for the System type under System.

Do 64-bit applications run faster?

The difference in performance between 32-bit and 64-bit versions of applications depends greatly upon their types, and the data types they are processing. But in general you may expect a 2-20% performance gain from mere recompilation of a program - this is explained by architectural changes in 64-bit processors [1].

What happens if you run a 32bit program on a 64-bit system?

Furthermore, if you run a 32-bit program on a 64-bit machine, it will run perfectly because the program will take the computer as if it was a 32-bit machine. This happens because the processor enters a special mode and hides all the 64-bit extensions and libraries.


1 Answers

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

like image 149
dhiraj suvarna Avatar answered Sep 21 '22 06:09

dhiraj suvarna