views:

1235

answers:

4

Hi,

How do I check whether an application/process is running in 32-bit or 64-bit mode? for eg. i will start a process abc.exe. In c# how can we know whether a 32-bit abc.exe is running or a 64-bit one? please help..

It is not the current process.. I am querying for some process 'abc.exe'

+15  A: 

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(string[] args)
        {
            foreach (Process p in Process.GetProcesses())
            {
                Console.WriteLine(p.ProcessName + " is " + (IsWin64(p) ? string.Empty : "not ") + "32-bit");
            }

            Console.ReadLine();
        }

        private static bool IsWin64(Process process)
        {
            if ((Environment.OSVersion.Version.Major > 5)
                || ((Environment.OSVersion.Version.Major == 5)
                    && (Environment.OSVersion.Version.Minor >= 1)))
            {
                IntPtr processHandle;
                bool retVal = false;

                try
                {
                    processHandle = Process.GetProcessById(process.Id).Handle;
                }
                catch
                {
                    return false; // access is denied to the process
                }

                if (!IsWow64Process(processHandle, out retVal))
                {
                    return false; // function failed
                }

                return retVal;
            }

            return false; // not on 64-bit Windows
        }

        [DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool IsWow64Process(
             [In] IntPtr hProcess,
             [Out] out bool wow64Process
             );
    }
}
Jesse C. Slicer
+1 for "The future is now!"
Dykam
Hi, this will work for the current instance. but I want to know for some other process which is already running. Pls help
satya
I'm amending this answer now.
Jesse C. Slicer
Thanks Jesse Slicer
satya
Ian Boyd
+4  A: 

You can check the size of a pointer to determine if it's 32bits or 64bits.

int bits = IntPtr.Size * 8;
Console.WriteLine( "{0}-bit", bits );
Console.ReadLine();
Darwyn
+1  A: 

If you're using .Net 4.0, it's a one-liner for the current process:

Environment.Is64BitProcess

http://msdn.microsoft.com/en-us/library/system.environment.is64bitprocess.aspx

Sam
Could you post the code of `Is64BitProcess`? Perhaps i can use what it does to figure out if i'm running in as a 64-bit process.
Ian Boyd