我的C#应用程序如何检查特定应用程序/进程(注意:不是当前进程)是以32位还是64位模式运行的?
例如,我可能想通过名称查询特定进程,比如`abc.exe`,或者根据进程ID号查询。
例如,我可能想通过名称查询特定进程,比如`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);
}
}
(Environment.OSVersion.Version.Major >= 5 && Environment.OSVersion.Version.Minor >= 1)
- Ian BoydprocessHandle = Process.GetProcessById(process.Id).Handle;
而不是只使用 processHandle = process.Handle;
? - Jonathon Reinhart如果您使用的是 .Net 4.0,则对于当前进程来说,这只需要一行代码:
Environment.Is64BitProcess
参见 Environment.Is64BitProcessProperty(MSDN)。
Is64BitProcess
的代码吗?也许我可以借鉴它的功能来判断当前进程是否为 64 位。 - Ian BoydIs64BitProcess
的代码(https://referencesource.microsoft.com/#mscorlib/system/environment.cs)。但是,它只是一个由编译符号控制的硬编码返回语句。 - Brian选择的答案是不正确的,因为它没有完成所要求的操作。它检查一个进程是否是在x64操作系统上运行的x86进程;所以对于在x64操作系统上运行的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);
}
Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") == "x86"
将始终返回true。如果.NET4受支持,最好使用System.Environment.Is64BitOperatingSystem
。 - Aizzat Suhardi你可以检查指针的大小来确定它是32位还是64位。
int bits = IntPtr.Size * 8;
Console.WriteLine( "{0}-bit", bits );
Console.ReadLine();
[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;
}
Process.GetProcessesByName("otherprocess.exe").First().Handle
,但使用 kernel32
中的 IsWow64Process
是这里的重要部分! - psulek以下是一行简易的检测。
bool is64Bit = IntPtr.Size == 8;
我喜欢使用这个:
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\";
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)
而不是硬编码 C:\Program Files\
真的那么难吗? - Luaan