Click here to Skip to main content
15,570,305 members
Articles / Programming Languages / C#
Tip/Trick
Posted 3 Feb 2015

Tagged as

Stats

25.4K views
10 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
4.21/5 (9 votes)
3 Feb 2015CPOL
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
cjb1103-Feb-15 21:54
cjb1103-Feb-15 21:54 
GeneralRe: My vote of 2 Pin
Péter V4-Feb-15 4:36
Péter V4-Feb-15 4:36 
GeneralMy vote of 5 Pin
Volynsky Alex3-Feb-15 11:36
professionalVolynsky Alex3-Feb-15 11:36 
GeneralRe: My vote of 5 Pin
Péter V3-Feb-15 12:54
Péter V3-Feb-15 12:54 
GeneralRe: My vote of 5 Pin
Volynsky Alex3-Feb-15 13:42
professionalVolynsky Alex3-Feb-15 13:42 
QuestionYou mean like this? Pin
Slacker0073-Feb-15 9:36
professionalSlacker0073-Feb-15 9:36 
AnswerRe: You mean like this? Pin
Péter V3-Feb-15 10:05
Péter V3-Feb-15 10:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.