Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

How to Detect 64bit OS/Process on .NET 2.0 to 3.5

4.21/5 (9 votes)
3 Feb 2015CPOL 28.3K  
How to detect 64 bit/process on .NET 2.0 to 3.5

Introduction

Simple function to detect:

  • Operation System is 64 bit?
  • Process is 64bit?

The Code

C#
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);

    /// <summary>
    /// Detect Operation System is 64bit
    /// </summary>
    /// <returns></returns>
    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)
                   );
        }
    }

    /// <summary>
    /// Detect Process is 64bit
    /// </summary>
    /// <returns></returns>
    public static bool Is64BitProc()
    {
        return (IntPtr.Size == 8);
    }
}

How to Use

C#
if(Misc.Is64BitOS())
{
    Console.WriteLine("Os is 64bit");
}
else
{
    Console.WriteLinve("Os is not 64bit"); // means 32bit at 2015-02-03 :)
}

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)