C# 如何以编程方式确定特定进程是 32 位还是 64 位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1953377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to determine programmatically whether a particular process is 32-bit or 64-bit
提问by satya
How can my C# application check whether a particular application/process (note: not the current process) is running in 32-bit or 64-bit mode?
我的 C# 应用程序如何检查特定应用程序/进程(注意:不是当前进程)是在 32 位还是 64 位模式下运行?
For example, I might want to query a particular process by name, i.e, 'abc.exe', or based on the process ID number.
例如,我可能想按名称(即“abc.exe”)或基于进程 ID 号查询特定进程。
采纳答案by Jesse C. Slicer
One of the more interesting ways I've seen is this:
我见过的更有趣的方法之一是:
if (IntPtr.Size == 4)
{
// 32-bit
}
else if (IntPtr.Size == 8)
{
// 64-bit
}
else
{
// The future is now!
}
To find out if OTHER processes are running in the 64-bit emulator (WOW64), use this code:
要确定其他进程是否正在 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);
}
}
回答by Darwyn
You can check the size of a pointer to determine if it's 32bits or 64bits.
您可以检查指针的大小以确定它是 32 位还是 64 位。
int bits = IntPtr.Size * 8;
Console.WriteLine( "{0}-bit", bits );
Console.ReadLine();
回答by Sam
If you're using .Net 4.0, it's a one-liner for the current process:
如果您使用 .Net 4.0,它是当前进程的单行:
Environment.Is64BitProcess
See Environment.Is64BitProcessProperty(MSDN).
请参阅Environment.Is64BitProcessProperty(MSDN)。
回答by user1351333
I like to use this:
我喜欢用这个:
string e = Environment.Is64BitOperatingSystem
This way if I need to locate or verify a file I can easily write:
这样,如果我需要定位或验证一个文件,我可以轻松地编写:
string e = Environment.Is64BitOperatingSystem
// If 64 bit locate the 32 bit folder
? @"C:\Program Files (x86)\"
// Else 32 bit
: @"C:\Program Files\";
回答by Vikram Bose
Here is the one line check.
这是单行检查。
bool is64Bit = IntPtr.Size == 8;
回答by Praveen M B
[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;
}
回答by user626528
The selected answer is incorrect as it doesn't do what was asked. It checks if a process is a x86 process running on x64 OS instead; so it will return "false" for a x64 process on x64 OS or x86 process running on x86 OS.
Also, it doesn't handle errors correctly.
所选答案不正确,因为它没有按照要求执行。它检查进程是否是在 x64 操作系统上运行的 x86 进程;因此它将为 x64 操作系统上的 x64 进程或 x86 操作系统上运行的 x86 进程返回“false”。
此外,它不能正确处理错误。
Here is a more correct method:
下面是一个更正确的方法:
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);
}