C# – How to determine processor 64 bit or 32 bit?

There are a lot of ways to find out which type of processor of your computer. Is it a 64 bit or 32 bit type? I would like to enumerate some methods which I know. You can use what you want.

1. Using environment variable

string strProcArchi = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
if (strProcArchi.IndexOf("64") > 0)
{
	Console.WriteLine("Processor 64 bit");
}
else
{
	Console.WriteLine("Processor 32 bit");
}

2. Using size of IntPtr (recommended)

if ( Marshal.SizeOf(typeof(IntPtr)) == 8 )
{
	Console.WriteLine("Processor 64 bit");
}
else
{
	Console.WriteLine("Processor 32 bit");
}

3. Using WMI

ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Processor");
foreach (ManagementObject queryObj in searcher.Get())
{
	if (queryObj["Architecture"].ToString() == "9")
	{
		Console.WriteLine("Processor 64 bit");
	}
	else
	{
		Console.WriteLine("Processor 32 bit");
	}
}

4. Reading registry

RegistryKey rkIdentifier = Registry.LocalMachine.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");
if (rkIdentifier.GetValue("Identifier").ToString().IndexOf("64") > 0)
{
	Console.WriteLine("Processor 64 bit");
}
else
{
	Console.WriteLine("Processor 32 bit");
}

5. Using GetSystemInfo API

[DllImport("kernel32.dll")]
public static extern void GetSystemInfo([MarshalAs(UnmanagedType.Struct)] ref SYSTEM_INFO lpSystemInfo);

[StructLayout(LayoutKind.Sequential)]
public struct SYSTEM_INFO
{
	internal _PROCESSOR_INFO_UNION uProcessorInfo;
	public uint dwPageSize;
	public IntPtr lpMinimumApplicationAddress;
	public IntPtr lpMaximumApplicationAddress;
	public IntPtr dwActiveProcessorMask;
	public uint dwNumberOfProcessors;
	public uint dwProcessorType;
	public uint dwAllocationGranularity;
	public ushort dwProcessorLevel;
	public ushort dwProcessorRevision;
}

[StructLayout(LayoutKind.Explicit)]
public struct _PROCESSOR_INFO_UNION
{
	[FieldOffset(0)]
	internal uint dwOemId;
	[FieldOffset(0)]
	internal ushort wProcessorArchitecture;
	[FieldOffset(2)]
	internal ushort wReserved;
}
public enum eOperatingSystem
{
	X64,
	x86,
	Itanium,
	Unknown
}

public eOperatingSystem DeterminateOperatingSystem()
{
	eOperatingSystem result = eOperatingSystem.Unknown;
	SYSTEM_INFO l_System_Info = new SYSTEM_INFO();
	GetSystemInfo(ref l_System_Info);

	switch (l_System_Info.uProcessorInfo.wProcessorArchitecture)
	{
		case 9: // PROCESSOR_ARCHITECTURE_AMD64
		result = eOperatingSystem.X64;
		break;
		case 6: // PROCESSOR_ARCHITECTURE_IA64
		result = eOperatingSystem.Itanium;
		break;
		case 0: // PROCESSOR_ARCHITECTURE_INTEL
		result = eOperatingSystem.x86;
		break;
		default: // PROCESSOR_ARCHITECTURE_UNKNOWN
		result = eOperatingSystem.Unknown;
		break;
	}
	return result;
}

6. Using IsWow64Process API

[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)] 
[return: MarshalAs(UnmanagedType.Bool)] 
public static extern bool IsWow64Process([In] IntPtr hProcess, [Out] out bool lpSystemInfo); 
...
bool bIsWow64;
bool bSuccess = IsWow64Process(Process.GetCurrentProcess().Handle, out bIsWow64);
if (!bSuccess)
	Console.WriteLine("Processor 32 bit");
else
{
	if (bIsWow64)
	{
		Console.WriteLine("Process 32 bit runs on processor 64 bit");
	}
	else
	{
		Console.WriteLine("Process 64 bit runs on processor 64 bit");
	}
}

If you know another way, then post here your code. I would like to know many other methods too.

2 thoughts on “C# – How to determine processor 64 bit or 32 bit?”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.