Programing

특정 프로세스가 32 비트인지 64 비트인지 프로그래밍 방식으로 확인하는 방법

lottogame 2020. 8. 27. 08:07
반응형

특정 프로세스가 32 비트인지 64 비트인지 프로그래밍 방식으로 확인하는 방법


내 C # 애플리케이션은 특정 애플리케이션 / 프로세스 (참고 : 현재 프로세스가 아님)가 32 비트 또는 64 비트 모드에서 실행 중인지 어떻게 확인할 수 있습니까?

예를 들어, 이름 (예 : 'abc.exe') 또는 프로세스 ID 번호를 기반으로 특정 프로세스를 쿼리 할 수 ​​있습니다.


내가 본 더 흥미로운 방법 중 하나는 다음과 같습니다.

if (IntPtr.Size == 4)
{
    // 32-bit
}
else if (IntPtr.Size == 8)
{
    // 64-bit
}
else
{
    // The future is now!
}

64 비트 에뮬레이터 (WOW64)에서 다른 프로세스가 실행 중인지 확인하려면 다음 코드를 사용하세요.

namespace Is64Bit
{
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Runtime.InteropServices;

    internal static class Program
    {
        private static void Main()
        {
            foreach (var p in Process.GetProcesses())
            {
                try
                {
                    Console.WriteLine(p.ProcessName + " is " + (p.IsWin64Emulator() ? string.Empty : "not ") + "32-bit");
                }
                catch (Win32Exception ex)
                {
                    if (ex.NativeErrorCode != 0x00000005)
                    {
                        throw;
                    }
                }
            }

            Console.ReadLine();
        }

        private static bool IsWin64Emulator(this Process process)
        {
            if ((Environment.OSVersion.Version.Major > 5)
                || ((Environment.OSVersion.Version.Major == 5) && (Environment.OSVersion.Version.Minor >= 1)))
            {
                bool retVal;

                return NativeMethods.IsWow64Process(process.Handle, out retVal) && retVal;
            }

            return false; // not on 64-bit Windows Emulator
        }
    }

    internal static class NativeMethods
    {
        [DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool IsWow64Process([In] IntPtr process, [Out] out bool wow64Process);
    }
}

.Net 4.0을 사용하는 경우 현재 프로세스에 대해 한 줄짜리입니다.

Environment.Is64BitProcess

Environment.Is64BitProcessProperty (MSDN)를 참조하십시오 .


The selected answer is incorrect as it doesn't do what was asked. It checks if a process is a x86 process running on x64 OS instead; so it will return "false" for a x64 process on x64 OS or x86 process running on x86 OS.
Also, it doesn't handle errors correctly.

Here is a more correct method:

internal static class NativeMethods
{
    // see https://msdn.microsoft.com/en-us/library/windows/desktop/ms684139%28v=vs.85%29.aspx
    public static bool Is64Bit(Process process)
    {
        if (!Environment.Is64BitOperatingSystem)
            return false;
        // if this method is not available in your version of .NET, use GetNativeSystemInfo via P/Invoke instead

        bool isWow64;
        if (!IsWow64Process(process.Handle, out isWow64))
            throw new Win32Exception();
        return !isWow64;
    }

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

You can check the size of a pointer to determine if it's 32bits or 64bits.

int bits = IntPtr.Size * 8;
Console.WriteLine( "{0}-bit", bits );
Console.ReadLine();

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

public static bool Is64Bit()
{
    bool retVal;

    IsWow64Process(Process.GetCurrentProcess().Handle, out retVal);

    return retVal;
}

Here is the one line check.

bool is64Bit = IntPtr.Size == 8;

I like to use this:

string e = Environment.Is64BitOperatingSystem

This way if I need to locate or verify a file I can easily write:

string e = Environment.Is64BitOperatingSystem

       // If 64 bit locate the 32 bit folder
       ? @"C:\Program Files (x86)\"

       // Else 32 bit
       : @"C:\Program Files\";

참고URL : https://stackoverflow.com/questions/1953377/how-to-determine-programmatically-whether-a-particular-process-is-32-bit-or-64-b

반응형