如何以编程方式确定特定进程是32位还是64位

sat*_*tya 97 c# process 32bit-64bit

我的C#应用​​程序如何检查特定的应用程序/进程(注意:当前进程)是否在32位或64位模式下运行?

例如,我可能想要按名称查询特定进程,即'abc.exe',或者根据进程ID号查询.

Jes*_*cer 174

我见过的一个比较有趣的方法是:

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

要确定在64位仿真器(WOW64)中是否正在运行OTHER进程,请使用以下代码:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • `(Environment.OSVersion.Version.Major> = 5 && Environment.OSVersion.Version.Minor> = 1)`这就是为什么微软必须创建版本谎言兼容性填充程序 - 以解决类似代码中的错误.Windows Vista(6.0)发布时会发生什么?然后人们嘲笑微软制造Windows 7版本6.1而不是7.0,它解决了许多app-compat错误. (8认同)
  • 我认为函数名称IsWin64有点误导.如果在x64 OS下运行32位进程,则返回true. (4认同)
  • 为什么要使用`processHandle = Process.GetProcessById(process.Id).Handle;`而不仅仅是`processHandle = process.Handle;`? (2认同)
  • 这个答案是不正确的;在错误的情况下返回 false 而不是引发异常是一个非常糟糕的设计。 (2认同)

小智 138

如果您使用的是.Net 4.0,它是当前流程的单行程序:

Environment.Is64BitProcess
Run Code Online (Sandbox Code Playgroud)

请参见Environment.Is64BitProcessProperty(MSDN).

  • OP特别要求查询*另一个*进程,而不是当前进程. (4认同)
  • @Ian有人为你完成了这项工作:http://stackoverflow.com/questions/336633/how-to-detect-windows-64-bit-platform-with-net/1913908#1913908 (3认同)
  • 您可以发布“ Is64BitProcess”的代码吗?也许我可以用它来确定我是否以64位进程运行。 (2认同)

use*_*528 18

所选答案不正确,因为它没有按要求进行操作.它检查进程是否是在x64 OS上运行的x86进程; 因此,对于在x86 OS或x86 OS上运行的x86进程上的x64进程,它将返回"false".
此外,它不能正确处理错误.

这是一个更正确的方法:

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);
}
Run Code Online (Sandbox Code Playgroud)


Dar*_*wyn 10

您可以检查指针的大小,以确定它是32位还是64位.

int bits = IntPtr.Size * 8;
Console.WriteLine( "{0}-bit", bits );
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)

  • 当这个答案第一次发布时,它不是很清楚,但OP想知道如何查询*另一个*进程而不是当前进程. (5认同)

小智 5

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

public static bool Is64Bit()
{
    bool retVal;

    IsWow64Process(Process.GetCurrentProcess().Handle, out retVal);

    return retVal;
}
Run Code Online (Sandbox Code Playgroud)

  • OP 特别询问如何查询*另一个*进程,而不是当前进程。 (6认同)