views:

93

answers:

4

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

A: 

The exception was thrown because the calling process was 32 bits. When I compiled it to x64 the problem was solved.

But I am still waiting for a better solution. My solution doesn't work if the executable file isn't in the appropriate ProgramFiles folder.

Jader Dias
+1  A: 

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
A: 

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

Esteban Araya
That only tells you if the calling process is 64-bit, I think the OP wants to know if another process is 64-bit
Phil Devaney
@Phil: Yeah, I wasn't sure what the OP's intent was. I figure he can downvote or comment.
Esteban Araya
This method you pointed is not included in .NET Fx
Jader Dias
+1  A: 

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
this method will fail in 32-bit Windows
Jader Dias
On 32-bit Windows all processes are 32-bit, so there's no need to do check. I've edited Is64BitProcess to reflect this.
Phil Devaney