Detect 32-Bit or 64-Bit of Windows

Detect 32-bit or 64-bit of Windows

The function to call is IsWow64Process or IsWow64Process2. It tells your 32-bit application if it is running on a 64 bit Windows.

If the program is compiled for 64 bits, it will already know.

How to detect Windows 64-bit platform with .NET?

UPDATE: As Joel Coehoorn and others suggest, starting at .NET Framework 4.0, you can just check Environment.Is64BitOperatingSystem.


IntPtr.Size won't return the correct value if running in 32-bit .NET Framework 2.0 on 64-bit Windows (it would return 32-bit).

As Microsoft's Raymond Chen describes, you have to first check if running in a 64-bit process (I think in .NET you can do so by checking IntPtr.Size), and if you are running in a 32-bit process, you still have to call the Win API function IsWow64Process. If this returns true, you are running in a 32-bit process on 64-bit Windows.

Microsoft's Raymond Chen:
How to detect programmatically whether you are running on 64-bit Windows

My solution:

static bool is64BitProcess = (IntPtr.Size == 8);
static bool is64BitOperatingSystem = is64BitProcess || InternalCheckIsWow64();

[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWow64Process(
[In] IntPtr hProcess,
[Out] out bool wow64Process
);

public static bool InternalCheckIsWow64()
{
if ((Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor >= 1) ||
Environment.OSVersion.Version.Major >= 6)
{
using (Process p = Process.GetCurrentProcess())
{
bool retVal;
if (!IsWow64Process(p.Handle, out retVal))
{
return false;
}
return retVal;
}
}
else
{
return false;
}
}

How to check if OS is 32 Bit OS or 64 Bit

Environment.Is64BitOperatingSystem should do nicely.

Determines whether the current operating system is a 64-bit operating system.

The assumption being that a false signifies a 32bit environment.

If you want to find out if the process is 64bit (as you can run a 32bit process on a 64bit OS), use Environment.Is64BitProcess:

Determines whether the current process is a 64-bit process.


Both of these have been introduced in .NET 4.0.

Detect whether current Windows version is 32 bit or 64 bit

See the batch script listed in How To Check If Computer Is Running A 32 Bit or 64 Bit Operating System. It also includes instructions for checking this from the Registry:

You can use the following registry location to check if computer is running 32 or 64 bit of Windows operating system:

HKLM\HARDWARE\DESCRIPTION\System\CentralProcessor\0

You will see the following registry entries in the right pane:

Identifier     REG_SZ             x86 Family 6 Model 14 Stepping 12
Platform ID REG_DWORD 0x00000020(32)

The above “x86” and “0x00000020(32)” indicate that the operating system version is 32 bit.

How can I identify if an OS is 64 bit or 32 bit with a windows batch script?

How about:

if "%PROCESSOR_ARCHITECTURE%" EQU "x86" (
rem 32 bit
) else (
rem 64 bit
)

There's a slight complication if someone's running the 32 bit command prompt on a 64 bit system (where it will identify as 32 bit, even though the OS is 64 bit), but that can be fixed using:

if "%PROCESSOR_ARCHITECTURE%" EQU "x86" (
if "%PROCESSOR_ARCHITEW6432%" EQU "AMD64" (
rem 64 bit OS, but running a 32 bit command prompt
) else (
rem 32 bit OS
)
) else (
rem 64 bit OS
)

Correct way to check if Windows is 64 bit or not, on runtime? (C++)

Here's what Raymond Chen suggests in his blog at https://devblogs.microsoft.com/oldnewthing/20050201-00/?p=36553:

BOOL Is64BitWindows()
{
#if defined(_WIN64)
return TRUE; // 64-bit programs run only on Win64
#elif defined(_WIN32)
// 32-bit programs run on both 32-bit and 64-bit Windows
// so must sniff
BOOL f64 = FALSE;
return IsWow64Process(GetCurrentProcess(), &f64) && f64;
#else
return FALSE; // Win64 does not support Win16
#endif
}


Related Topics



Leave a reply



Submit