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

问题描述:

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

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

+0

请始终把语言作为一个标签;现在在这篇文章中我会改变它。 :-) – 2009-12-23 15:21:56

+3

请说明您是否想知道**当前**进程是64位还是您正在查询另一个进程? – 2009-12-23 15:25:42

+0

Dupelicate:http://*.com/questions/266082/how-do-i-tell-if-my-application-is-running-as-a-32-or-64-bit-application – 2009-12-23 17:02:39

一个我见过的更有趣的方法是这样的:

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

+1 for “未来是现在!” – Dykam 2009-12-24 08:40:10

+0

嗨,这将适用于当前的情况。但我想知道其他正在运行的其他进程。请帮助 – satya 2010-01-04 08:01:49

+0

我现在正在修改此答案。 – 2010-01-04 13:53:29

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

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

当时这个答案是第一次发布它不是很清楚,但OP想知道如何查询*另一个*进程,而不是当前进程。 – 2015-10-19 03:10:59

如果你使用.NET 4.0,这是一个班轮当前进程:

Environment.Is64BitProcess 

Environment.Is64BitProcessProperty(MSDN)。

+1

你可以发布'Is64BitProcess'的代码吗?也许我可以使用它来确定我是否以64位进程运行。 – 2010-09-09 14:48:47

+1

@Ian,我怀疑山姆会合法地允许在这个论坛上发布MS代码。我不确定他们的参考许可证的确切内容,但我确定它禁止在任何地方复制代码。 – ProfK 2011-03-28 06:43:53

+3

@Ian某人已经为你完成了这项工作:http://*.com/questions/336633/how-to-detect-windows-64-bit-platform-with-net/1913908#1913908 – 2011-09-05 14:01:07

我喜欢用这样的:

string e = Environment.Is64BitOperatingSystem 

这样,如果我需要找到或验证的文件,我可以轻松地写出:

string e = Environment.Is64BitOperatingSystem 

     // If 64 bit locate the 32 bit folder 
     ? @"C:\Program Files (x86)\" 

     // Else 32 bit 
     : @"C:\Program Files\"; 
+13

在64位OS机器上32位进程呢? – Kiquenet 2012-08-23 12:30:18

+2

假设驱动器号似乎也是错误的 – Epirocks 2015-09-02 11:43:09

+2

是否真的很难使用'Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)'而不是硬编码'C:\ Program Files \'? – Luaan 2015-10-19 11:12:29

下面是一行检查。

bool is64Bit = IntPtr.Size == 8; 
+4

该OP特别询问如何查询*另一个*进程,而不是当前进程。 – 2015-10-19 03:08:55

[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; 
} 
+2

OP特意询问如何查询*另一个*进程,而不是当前进程。 – 2015-10-19 03:08:35

所选答案不正确,因为它不会做什么被问。它会检查进程是否是在x64操作系统上运行的x86进程;因此它将在x86操作系统上运行的x64 OS或x86进程上返回“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); 
} 
+0

对于32位进程,Environment.GetEnvironmentVariable(“PROCESSOR_ARCHITECTURE”)==“x86”'将始终返回true。 如果支持.NET4,最好使用'System.Environment.Is64BitOperatingSystem' – 2017-10-23 03:23:05

+0

@Aizzat Suhardi,好的谢谢。 – user626528 2017-10-23 16:05:35