Introduction
Simple function to detect:
- Operation System is 64 bit?
- Process is 64bit?
The Code
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
public static class Misc
{
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsWow64Process([In] IntPtr hProcess, [Out] out bool lpSystemInfo);
public static bool Is64BitOS()
{
using (Process p = Process.GetCurrentProcess())
{
bool retVal;
return (IntPtr.Size == 8 ||
(IsWow64Process != null && IntPtr.Size == 4 &&
IsWow64Process(p.Handle, out retVal) && retVal)
);
}
}
public static bool Is64BitProc()
{
return (IntPtr.Size == 8);
}
}
How to Use
if(Misc.Is64BitOS())
{
Console.WriteLine("Os is 64bit");
}
else
{
Console.WriteLinve("Os is not 64bit");
}
if(Misc.Is64BitProc())
{
Console.WriteLine("Process is 64bit");
}
Notes
IntPtr.Size
is 8 in 64bit Process and 4 in 32bit Process.
"WOW64
is the x86 emulator that allows 32-bit Windows-based applications to run seamlessly on 64-bit Windows."
IsWow64Process
detects 32bit process runs in Wow64
.
.NET 4, and .NET 4.5
System.Environment.Is64BitOperatingSystem
for checking OS
System.Environment.Is64BitProcess
for checking Process