How to determine programmatically whether a particular process is 32-bit or 64-bit
One of the more interesting ways I've seen is this:
if (IntPtr.Size == 4)
{
// 32-bit
}
else if (IntPtr.Size == 8)
{
// 64-bit
}
else
{
// The future is now!
}
To find out if OTHER processes are running in the 64-bit emulator (WOW64), use this code:
namespace Is64Bit
{
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
internal static class Program
{
private static void Main()
{
foreach (var p in Process.GetProcesses())
{
try
{
Console.WriteLine(p.ProcessName + " is " + (p.IsWin64Emulator() ? string.Empty : "not ") + "32-bit");
}
catch (Win32Exception ex)
{
if (ex.NativeErrorCode != 0x00000005)
{
throw;
}
}
}
Console.ReadLine();
}
private static bool IsWin64Emulator(this Process process)
{
if ((Environment.OSVersion.Version.Major > 5)
|| ((Environment.OSVersion.Version.Major == 5) && (Environment.OSVersion.Version.Minor >= 1)))
{
bool retVal;
return NativeMethods.IsWow64Process(process.Handle, out retVal) && retVal;
}
return false; // not on 64-bit Windows Emulator
}
}
internal static class NativeMethods
{
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool IsWow64Process([In] IntPtr process, [Out] out bool wow64Process);
}
}
How can I tell if another process is 64bit?
I don't think there is a 100% definitive way to know without a PInvoke.
But one item which may work though is to inspect the set of loaded modules (Process.Modules
). If the primary modules (user32, kernel32, etc ...) come from the Wow64 directory and you're running an a 64 bit machine then there is a solid chance it's a 32 bit process. If they don't come from the Wow64 directory and it's a 64 bit machine then it's likely it's a 64 bit process.
Again not definitive but a good estimate.
How do I tell if my application is running as a 32-bit or 64-bit application?
if (IntPtr.Size == 8)
{
// 64 bit machine
}
else if (IntPtr.Size == 4)
{
// 32 bit machine
}
How to determine whether a System.Diagnostics.Process is 32 or 64 bit?
You need to call IsWow64Process via P/Invoke:
[DllImport( "kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi )]
[return: MarshalAs( UnmanagedType.Bool )]
public static extern bool IsWow64Process( [In] IntPtr processHandle, [Out, MarshalAs( UnmanagedType.Bool )] out bool wow64Process );
Here's a helper to make it a bit easier to call:
public static bool Is64BitProcess( this Process process )
{
if ( !Environment.Is64BitOperatingSystem )
return false;
bool isWow64Process;
if ( !IsWow64Process( process.Handle, out isWow64Process ) )
throw new Win32Exception( Marshal.GetLastWin32Error() );
return !isWow64Process;
}
How to know a process of an app is 32-bit or 64-bit programmatically in Android lollipop?
Try System.getProperty("os.arch")
.
I haven't tried it on 64-bit android, but it must return something like 'aarch64' in case of 64 bit device.
http://developer.android.com/reference/java/lang/System.html#getProperty(java.lang.String)
How can I check if a given address is 32-bit or 64-bit?
You can use an AND operation to check if any of the higher 32 bits are set. Assuming that you have ulong
because addresses are not negative:
if ((x & 0xFFFFFFFF00000000UL) == 0)
Console.WriteLine("x would fit into 32 bit");
Get bitness (32-bit/64-bit) from Process object
To get your bitness you will have to call some native methods. A good writeup on how to do this can be found in this StackOverflow answer
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.
How to determine programmatically whether a particular process is 32-bit or 64-bit
One of the more interesting ways I've seen is this:
if (IntPtr.Size == 4)
{
// 32-bit
}
else if (IntPtr.Size == 8)
{
// 64-bit
}
else
{
// The future is now!
}
To find out if OTHER processes are running in the 64-bit emulator (WOW64), use this code:
namespace Is64Bit
{
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
internal static class Program
{
private static void Main()
{
foreach (var p in Process.GetProcesses())
{
try
{
Console.WriteLine(p.ProcessName + " is " + (p.IsWin64Emulator() ? string.Empty : "not ") + "32-bit");
}
catch (Win32Exception ex)
{
if (ex.NativeErrorCode != 0x00000005)
{
throw;
}
}
}
Console.ReadLine();
}
private static bool IsWin64Emulator(this Process process)
{
if ((Environment.OSVersion.Version.Major > 5)
|| ((Environment.OSVersion.Version.Major == 5) && (Environment.OSVersion.Version.Minor >= 1)))
{
bool retVal;
return NativeMethods.IsWow64Process(process.Handle, out retVal) && retVal;
}
return false; // not on 64-bit Windows Emulator
}
}
internal static class NativeMethods
{
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool IsWow64Process([In] IntPtr process, [Out] out bool wow64Process);
}
}
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.
Related Topics
Http Error 500.30 - Ancm In-Process Start Failure
Invert "If" Statement to Reduce Nesting
How to Convert This Foreach Code to Parallel.Foreach
Parsing Large JSON File in .Net
Embedding Unmanaged Dll into a Managed C# Dll
How to Get the File Size from Http Headers
Impossible to Use Ref and Out for First ("This") Parameter in Extension Methods
C - Determine If a Number Is Prime
Unloading the Assembly Loaded with Assembly.Loadfrom()
Replace "\\" with "\" in a String in C#
How to Run an Exe Program from a Windows Service Using C#
What Is [Serializable] and When Should I Use It
Omitting All Xsi and Xsd Namespaces When Serializing an Object in .Net
Two Different Dll with Same Namespace
Shorter Syntax for Casting from a List<X> to a List<Y>
A Field Initializer Cannot Reference the Non-Static Field, Method, or Property