Determining the Bitness of a Process
In C#, determining whether a particular process is running in 32-bit or 64-bit mode is achieved through various methods.
IntPtr Size Check
The simplest approach involves checking the size of the IntPtr data type:
if (IntPtr.Size == 4) { // 32-bit process } else if (IntPtr.Size == 8) { // 64-bit process }
WOW64 Check
To ascertain whether other processes are running in the 64-bit emulator (WOW64), consider the following code:
private static bool IsWin64Emulator(this Process process) { if ((Environment.OSVersion.Version.Major > 5) || ((Environment.OSVersion.Version.Major == 5) && (Environment.OSVersion.Version.Minor >= 1))) { bool retVal; return NativeMethods.IsWow64Process(process.Handle, out retVal) && retVal; } return false; // not on 64-bit Windows Emulator }
where NativeMethods.IsWow64Process is a DLL import:
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool IsWow64Process([In] IntPtr process, [Out] out bool wow64Process);
The above is the detailed content of How Can I Determine if a C# Process is 32-bit or 64-bit?. For more information, please follow other related articles on the PHP Chinese website!