Tuesday 7 May 2013

Get Windows Architecture in Dot NET

How to Determine if 64bit or 32bit OS Version


There are a lot of ways to Get Windows Architecture in .NET, the .NET Framework v4.0 already has this method on their libraries, but many people still are using previous versions (as v3.5 or v2.0).

For .NET v4.0 or higher, it is really simple, just call the ‘Environment.Is64BitOperatingSystem’ property and check if is true or false.:

C# Code :
if (Environment.Is64BitOperatingSystem)
    Console.WriteLine("Your OS is 64 bits.");
else Console.WriteLine("Your OS is 32 bits.");

But for the people using v3.5 or previous, We have this simple way compatible with ‘x86′, ‘x64′ and ‘AnyCPU’ architectures:

//C# Code :
public static bool Is64BitOperatingSystem()
{
    if (!Environment.GetFolderPath(Environment.SpecialFolder.SystemX86).ToUpper().Contains("SYSTEM32"))
        return true; //Check for the SystemX86 variable. If is not 'SYSTEM32' then the Operating System is x64 (SystemX86 = SYSWOW64).
    return false; //Return false when the method was not blocked by the if statement.
}


Public Shared Function Is64BitOperatingSystem() As Boolean
    If Not Environment.GetFolderPath(Environment.SpecialFolder.SystemX86).ToUpper().Contains("SYSTEM32") Then
        Return True
    End If
    'Check for the SystemX86 variable. If is not 'SYSTEM32' then the Operating System is x64 (SystemX86 = SYSWOW64).
    Return False
    'Return false when the method was not blocked by the if statement.
End Function

We also have a way to do it by checking the IntPtr Size:

if (IntPtr.Size == 8)
    Console.WriteLine("Your OS is 64 bits.");
else Console.WriteLine("Your OS is 32 bits.");

‘IntPtr.Size’ always will be eight when the process is running in memory as x64, but only will work if your project was made using the ‘AnyCPU’ platform.


For another Class that using 'kernel32'

//C# Code :
using System;
using System.Runtime.InteropServices;
 
namespace com0do99.library
{
    public static class OsVersionHelper
    {
 
        // Will return result of 64but OS on all versions of windows that support .net
        public static bool Is64BitOperatingSystem()
        {
            if ( IntPtr.Size == 8 ) //64bit will only run on 64bit
            {
                return true;
            }
            bool flag;
            return (DoesWin32MethodExist("kernel32.dll", "IsWow64Process") && IsWow64Process(GetCurrentProcess(), out flag)) && flag;
        }
 
        private static bool DoesWin32MethodExist(string moduleName, string methodName)
        {
            IntPtr moduleHandle = GetModuleHandle(moduleName);
            if (moduleHandle == IntPtr.Zero)
            {
                return false;
            }
 
            return GetProcAddress(moduleHandle, methodName) != IntPtr.Zero;
        }
 
        [DllImport("kernel32.dll")]
        private static extern IntPtr GetCurrentProcess();
 
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr GetModuleHandle(string moduleName);
 
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetProcAddress(IntPtr module, [MarshalAs(UnmanagedType.LPStr)]string procName);
 
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool IsWow64Process(IntPtr process, out bool wow64Process);
    }
}



Thank You