我的C#應用程序如何檢查特定應用程序/進程(注:不是當前進程)是以32位還是64位模式運行?如何以編程方式確定特定進程是32位還是64位
例如,我可能想要按名稱查詢特定進程,即'abc.exe',或者根據進程ID號來查詢。
我的C#應用程序如何檢查特定應用程序/進程(注:不是當前進程)是以32位還是64位模式運行?如何以編程方式確定特定進程是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);
}
}
您可以檢查指針的大小以確定它是32位還是64位。
int bits = IntPtr.Size * 8;
Console.WriteLine("{0}-bit", bits);
Console.ReadLine();
當時這個答案是第一次發佈它不是很清楚,但OP想知道如何查詢*另一個*進程,而不是當前進程。 – 2015-10-19 03:10:59
你可以發佈'Is64BitProcess'的代碼嗎?也許我可以使用它來確定我是否以64位進程運行。 – 2010-09-09 14:48:47
@Ian,我懷疑山姆會合法地允許在這個論壇上發佈MS代碼。我不確定他們的參考許可證的確切內容,但我確定它禁止在任何地方複製代碼。 – ProfK 2011-03-28 06:43:53
@Ian某人已經爲你完成了這項工作:http://stackoverflow.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\";
下面是一行檢查。
bool is64Bit = IntPtr.Size == 8;
該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;
}
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);
}
對於32位進程,Environment.GetEnvironmentVariable(「PROCESSOR_ARCHITECTURE」)==「x86」'將始終返回true。 如果支持.NET4,最好使用'System.Environment.Is64BitOperatingSystem' – 2017-10-23 03:23:05
@Aizzat Suhardi,好的謝謝。 – user626528 2017-10-23 16:05:35
請始終把語言作爲一個標籤;現在在這篇文章中我會改變它。 :-) – 2009-12-23 15:21:56
請說明您是否想知道**當前**進程是64位還是您正在查詢另一個進程? – 2009-12-23 15:25:42
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