2009-12-23 117 views
84

我的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://stackoverflow.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

回答

151

一個我見過的更有趣的方法是這樣的:

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

10

您可以檢查指針的大小以確定它是32位還是64位。

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

當時這個答案是第一次發佈它不是很清楚,但OP想知道如何查詢*另一個*進程,而不是當前進程。 – 2015-10-19 03:10:59

127

如果你使用.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://stackoverflow.com/questions/336633/how-to-detect-windows-64-bit-platform-with-net/1913908#1913908 – 2011-09-05 14:01:07

0

我喜歡用這樣的:

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

1

下面是一行檢查。

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

該OP特別詢問如何查詢*另一個*進程,而不是當前進程。 – 2015-10-19 03:08:55

4
[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

13

所選答案不正確,因爲它不會做什麼被問。它會檢查進程是否是在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

相關問題