sat*_*tya 97 c# process 32bit-64bit
我的C#应用程序如何检查特定的应用程序/进程(注意:当前进程)是否在32位或64位模式下运行?
例如,我可能想要按名称查询特定进程,即'abc.exe',或者根据进程ID号查询.
Jes*_*cer 174
我见过的一个比较有趣的方法是:
if (IntPtr.Size == 4)
{
// 32-bit
}
else if (IntPtr.Size == 8)
{
// 64-bit
}
else
{
// The future is now!
}
Run Code Online (Sandbox Code Playgroud)
要确定在64位仿真器(WOW64)中是否正在运行OTHER进程,请使用以下代码:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 138
如果您使用的是.Net 4.0,它是当前流程的单行程序:
Environment.Is64BitProcess
Run Code Online (Sandbox Code Playgroud)
请参见Environment.Is64BitProcessProperty(MSDN).
use*_*528 18
所选答案不正确,因为它没有按要求进行操作.它检查进程是否是在x64 OS上运行的x86进程; 因此,对于在x86 OS或x86 OS上运行的x86进程上的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);
}
Run Code Online (Sandbox Code Playgroud)
Dar*_*wyn 10
您可以检查指针的大小,以确定它是32位还是64位.
int bits = IntPtr.Size * 8;
Console.WriteLine( "{0}-bit", bits );
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)
小智 5
[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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
80057 次 |
| 最近记录: |