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

标签 c# process 32bit-64bit

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

例如,我可能想按名称(即“abc.exe”)或基于进程 ID 号查询特定进程。

最佳答案

我见过的更有趣的方法之一是:

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

要查明其他进程是否正在 64 位模拟器 (WOW64) 中运行,请使用以下代码:

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

关于c# - 如何以编程方式确定特定进程是 32 位还是 64 位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1953377/

相关文章:

c# - 如何创建类似于 Visual Studio 2008 安装程序的安装程序

c# - 从 Azure Function 中读取日志

c# - 如何将 runat=server 属性添加到 html 按钮

一个进程可以有多个标准输出和标准输入吗?

c# - 在特定屏幕上启动进程

c - C中的fork过程

sql-server - 如何在 64 位包中执行 32 位 SSIS 包?

c# - 按 "X"按钮时允许取消

linux - 是否可以在 64 位 Linux 的同一个可执行文件中同时使用 64 位和 32 位指令?

c++ - 32 位和 64 位程序可以使用 MSMQ 进行通信吗?