How to determine whether a System.Diagnostics.Process is 32 or 64 bit?

ぐ巨炮叔叔 提交于 2019-11-30 20:56:57

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;
}

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!