8

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

Jader Dias
  • 88,211
  • 155
  • 421
  • 625
  • 2
    You are asking the wrong question. Real question should be: "how did I screw up the ReadProcessMemory call?" – Hans Passant Aug 26 '10 at 15:20
  • 2
    @Hans I don't care at all about this call as long as the question title is answered. The problem I listed is just a method of answering the title. – Jader Dias Aug 26 '10 at 16:00
  • possible duplicate of [How to know a process is 32-bit or 64-bit programmatically](http://stackoverflow.com/questions/1953377/how-to-know-a-process-is-32-bit-or-64-bit-programmatically) – Jesse C. Slicer Aug 26 '10 at 16:15
  • @Jesse the question you pointed asks about the current process, not another process. – Jader Dias Aug 26 '10 at 16:23
  • look at my answer. It takes into account other processes as the OP wasn't clear. – Jesse C. Slicer Aug 26 '10 at 16:33

3 Answers3

12

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;
}
Phil Devaney
  • 17,607
  • 6
  • 41
  • 33
1

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.

Richard
  • 106,783
  • 21
  • 203
  • 265
  • also fails if running in 32-bit proces while accessing modules of 64-bit process with Win32Exception "A 32 bit processes cannot access modules of a 64 bit process." – tibx Apr 17 '21 at 13:11
1

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

Esteban Araya
  • 29,284
  • 24
  • 107
  • 141