C# 如何以编程方式确定特定进程是32位还是64位
我的C#应用程序如何检查特定的应用程序/进程(注意:不是当前进程)是以32位还是64位模式运行C# 如何以编程方式确定特定进程是32位还是64位,c#,process,32bit-64bit,C#,Process,32bit 64bit,我的C#应用程序如何检查特定的应用程序/进程(注意:不是当前进程)是以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)中是否正在运行其他进程,
例如,我可能希望按名称(即“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();
如果您使用的是.Net 4.0,则它是当前流程的一行程序:
Environment.Is64BitProcess
请参阅(MSDN)。我喜欢使用这个:
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;
所选答案不正确,因为它不符合要求。它检查一个进程是否是在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);
}
请始终将语言作为标签输入;我现在将在这篇文章中对此进行更改。:-)请澄清您是想知道当前进程是64位还是正在查询另一个进程?Dupelicate:您能发布
is64位进程的代码吗?也许我可以利用它的功能来判断我是否以64位进程的形式运行。@Ian,我怀疑Sam是否合法地被允许在这个论坛上发布MS代码。我不确定他们的参考许可证的确切内容,但我很确定它禁止在任何地方复制代码。@Ian有人为你做了这项工作:OP特别要求查询另一个进程,而不是当前进程。请注意,Microsoft确实发布了Is64BitProcess
()的代码。然而,它只是一个硬编码的返回语句,由编译符号控制。(Environment.OSVersion.Version.Major>=5&&Environment.OSVersion.Version.Minor>=1)
,这就是为什么微软必须创建版本兼容性垫片来解决类似代码中的错误。当WindowsVista(6.0)问世时会发生什么?然后,人们就因为微软将Windows7版本改为6.1而不是7.0而诋毁了它,因为它修复了太多的应用程序兼容性错误。我认为函数名为Win64有点误导。如果32位进程在x64操作系统下运行,则返回true
而不仅仅是processHandle=process.Handle代码>?@JonathonReinhart这不是个好问题吗。我不知道。从一种方式转换到另一种方式,这一定是一种退化。谢谢你找到那个!这个答案是不正确的;在出现错误时返回false而不是引发异常是一种非常糟糕的设计。64位操作系统机器中的32位进程如何?使用Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)
而不是硬编码`C:\Program Files`?永远不要硬编码“Program Files”,因为它是一个可本地化的字符串。ΑρχείαΕφαρμογών,Arquivos de Programas等。OP特别询问如何查询另一个流程,而不是当前流程。OP特别询问如何查询另一个流程,而不是当前流程。在第一次发布此答案时,并不十分清楚,但是OP想知道如何查询另一个进程而不是当前进程。Environment.GetEnvironmentVariable(“PROCESSOR_ARCHITECTURE”)=“x86”
对于32位进程总是返回true。如果支持.NET4,最好使用System.Environment.is64位操作系统
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);
}