How to determine whether a System.Diagnostics.Proc

2019-02-17 16:51发布

I tried:

process.MainModule.FileName.Contains("x86")

But it threw an exception for a x64 process:

Win32Exception: Only a part of the ReadProcessMemory ou WriteProcessMemory request finished

3条回答
狗以群分
2楼-- · 2019-02-17 17:09

You need to call IsWow64Process via P/Invoke:

[DllImport( "kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi )]
[return: MarshalAs( UnmanagedType.Bool )]
public static extern bool IsWow64Process( [In] IntPtr processHandle, [Out, MarshalAs( UnmanagedType.Bool )] out bool wow64Process );

Here's a helper to make it a bit easier to call:

public static bool Is64BitProcess( this Process process )
{
    if ( !Environment.Is64BitOperatingSystem )
        return false;

    bool isWow64Process;
    if ( !IsWow64Process( process.Handle, out isWow64Process ) )
        throw new Win32Exception( Marshal.GetLastWin32Error() );

    return !isWow64Process;
}
查看更多
啃猪蹄的小仙女
3楼-- · 2019-02-17 17:26

Neither WMI's Win32_Process or System.Diagnostics.Process offer any explicit property.

How about iterating through the loaded modules (Process.Modules), a 32bit process will have loaded %WinDir%\syswow64\kernel32.dll while a 64bit process will have loaded it from %WinDir%\system32\kernel32.dll (this is the one dll that every Windows process loads).

NB. This test will, of course, fail on a x86 OS instance.

查看更多
来,给爷笑一个
4楼-- · 2019-02-17 17:26

Environment.Is64BitProcess is probably what you're looking for.

查看更多
登录 后发表回答