BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

System Information
.NET 1.1+

Checking if the Current Process is 64-bit

Some software must behave differently depending upon whether it is running as a 32-bit or 64-bit process. .NET applications can be compiled to work in either mode, so detecting the execution mode is important.

32-Bit and 64-Bit Processes

When you develop a .NET application, it can be built to run on a specific architecture or for any processor. When compiled for x86, the software will run on any Windows-based computer that supports the targeted .NET framework version. In this case the software will always run in a 32-bit process, even on a 64-bit operating system.

If you compile the application for x64 architecture, the software will require a 64-bit process and will not run on 32-bit operating systems. Both the x86 and x64 options can be useful when you are using third party DLLs, particularly those that use unmanaged code, that require either a 32-bit or a 64-bit process. For example, when a third-party DLL is compiled for x86, a 64-bit process cannot use it.

If you compile your assemblies using the Any CPU option, the code will execute on any supported operating system and the process will use the most appropriate architecture option; on 32-bit operating systems it will run as 32-bit and on 64-bit operating systems your software will execute in a 64-bit process. This generates the possibility that the code will need to behave differently according to its process type. You'll then need to detect the type of process at run time.

Environment.Is64BitProcess

If you are using the .NET framework version 4.0, detecting the process type is as simple as reading a static property of the Environment class. The property is named, "Is64BitProcess". It returns a Boolean value of true if the process is 64-bit and false otherwise.

The following code checks the property and adjusts the outputted message according to the process type. On a 32-bit operating system this will show the message, "32-bit process". On a 64-bit operating system it will show "32-bit process" when compiled for x32 architecture and "64-bit process" when compiled for x64 or "Any CPU".

if (Environment.Is64BitProcess)
    Console.WriteLine("64-bit process");
else
    Console.WriteLine("32-bit process");

IntPtr.Size

If you are using an older version of the .NET framework you have to take an alternative approach. One easy way is to use the IntPtr structure. This structure is designed to hold a platform-specific integer value, which will be four bytes for a 32-bit process and eight bytes for 64-bit. The structure provides a handy property named, "Length", which returns the number of bytes it holds.

We can use this member to recreate the Is64BitProcess property in a new class for use in earlier framework versions, as follows:

public class Check64
{
    public static bool Is64BitProcess
    {
        get { return IntPtr.Size == 8; }
    }
}

The new member can now be used in the same manner as the property from the Environment class:

if (Check64.Is64BitProcess)
    Console.WriteLine("64-bit process");
else
    Console.WriteLine("32-bit process");
5 September 2012