How do I know if my application works as a 32-bit or 64-bit application?

How do I know if my application (compiled in Visual Studio 2008 as Any CPU) works as a 32-bit or 64-bit application?

+56
c # 64bit 32-bit
Nov 05 '08 at 18:08
source share
5 answers
if (IntPtr.Size == 8) { // 64 bit machine } else if (IntPtr.Size == 4) { // 32 bit machine } 
+59
Dec 29 '09 at 13:09
source share

If you are using .NET 4.0, this is one line for the current process:

 Environment.Is64BitProcess 

Ref: Environment.Is64BitProcess Property (MSDN)

+127
Aug 11 '10 at 18:25
source share

I found this code from Martijn Boven that does the trick:

 public static bool Is64BitMode() { return System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)) == 8; } 
+5
Nov 05 '08 at 18:09
source share

This sample code from the Microsoft All-In-One Code Framework may answer your question:

Detecting a running process platform in C # (CSPlatformDetector)

The CSPlatformDetector sample code demonstrates the following tasks: related to platform discovery:

  • Determine the name of the current operating system. (e.g. "Microsoft Windows 7 Enterprise")
  • Detect the version of the current operating system. (for example, "Microsoft Windows NT 6.1.7600.0")
  • Determine if the current operating system is a 64-bit operating system.
  • Determine if the current process is a 64-bit process.
  • Determine if an arbitrary process running on a system is 64-bit.

If you just want to determine if the current running process is a 64-bit process, you can use the Environment.Is64BitProcess property, which is new in .NET. Frames 4.

And if you want to determine whether an arbitrary application is running on the system, this is a 64-bit process, you need to determine the bit of the operating system, and if it is 64-bit, call IsWow64Process() with the handle to the target process:

 static bool Is64BitProcess(IntPtr hProcess) { bool flag = false; if (Environment.Is64BitOperatingSystem) { // On 64-bit OS, if a process is not running under Wow64 mode, // the process must be a 64-bit process. flag = !(NativeMethods.IsWow64Process(hProcess, out flag) && flag); } return flag; } 
+5
Sep 04 2018-11-11T00:
source share

In the .Net standard, you can use System.Runtime.InteropServices.RuntimeInformation.OSArchitecture

0
Aug 10 '17 at 15:00
source share



All Articles