So bestimmen Sie programmgesteuert, ob ein bestimmter Prozess 32-Bit oder 64-Bit ist

So bestimmen Sie programmgesteuert, ob ein bestimmter Prozess 32-Bit oder 64-Bit ist

Eine der interessanteren Möglichkeiten, die ich gesehen habe, ist diese:

if (IntPtr.Size == 4)
{
    // 32-bit
}
else if (IntPtr.Size == 8)
{
    // 64-bit
}
else
{
    // The future is now!
}

Verwenden Sie diesen Code, um herauszufinden, ob ANDERE Prozesse im 64-Bit-Emulator (WOW64) ausgeführt werden:

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);
    }
}

Wenn Sie .Net 4.0 verwenden, ist es ein Einzeiler für den aktuellen Prozess:

Environment.Is64BitProcess

Siehe Environment.Is64BitProcessProperty (MSDN).


Die ausgewählte Antwort ist falsch, da sie nicht das tut, was gefragt wurde. Es prüft, ob es sich bei einem Prozess um einen x86-Prozess handelt, der stattdessen auf einem x64-Betriebssystem ausgeführt wird. Daher wird für einen x64-Prozess auf einem x64-Betriebssystem oder einen x86-Prozess, der auf einem x86-Betriebssystem ausgeführt wird, „false“ zurückgegeben.
Außerdem behandelt es Fehler nicht richtig.

Hier ist eine korrektere Methode:

internal static class NativeMethods
{
    // see https://msdn.microsoft.com/en-us/library/windows/desktop/ms684139%28v=vs.85%29.aspx
    public static bool Is64Bit(Process process)
    {
        if (!Environment.Is64BitOperatingSystem)
            return false;
        // if this method is not available in your version of .NET, use GetNativeSystemInfo via P/Invoke instead

        bool isWow64;
        if (!IsWow64Process(process.Handle, out isWow64))
            throw new Win32Exception();
        return !isWow64;
    }

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