How to determine if System.Diagnostics.Process is 32 or 64 bits?

I tried:

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

But this threw an exception for the x64 process:

Win32Exception: only part of the ReadProcessMemory ou WriteProcessMemory request has completed

+5
source share
3 answers

You need to call IsWow64Process through 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 little easier:

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;
}
+8
source

Neither WMI Win32_Processeither System.Diagnostics.Processoffers any explicit property.

(Process.Modules), 32- %WinDir%\syswow64\kernel32.dll, 64- %WinDir%\system32\kernel32.dll ( DLL, Windows).

NB. , x86.

+1

Environment.Is64BitProcess - , , , .

0

All Articles