2

Как в C# узнать разрядность exe файла??? Если это не возможно, то как узнать разрядность процесса - 32бит или 64бит?

2
  • своего или чужого?
    – Qwertiy
    28 дек 2017 в 15:11
  • Чужого процесса или exe
    – XXX
    28 дек 2017 в 15:16

2 ответа 2

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

Ссылка на источник ссылка

Примечание. Код ошибки 0x00000005 - ERROR_ACCESS_DENIED (возникает при попытке обратится к системному процессу без привилегий администратора).

1
  • Спасибо! С наступающим! :)
    – XXX
    28 дек 2017 в 15:17
1

Для EXE-файла можно узнать разрядность с помощью функции GetBinaryType

using System;
using System.Runtime.InteropServices;

public enum BinaryType : uint
{
    SCS_32BIT_BINARY = 0,
    SCS_64BIT_BINARY = 6,
    SCS_DOS_BINARY = 1,
    SCS_OS216_BINARY = 5,
    SCS_PIF_BINARY = 3,
    SCS_POSIX_BINARY = 4,
    SCS_WOW_BINARY = 2
}

public class MyClass
{
    [DllImport("kernel32.dll")]
    public static extern bool GetBinaryType(string lpApplicationName,
       out BinaryType lpBinaryType);

    void PrintExeType(string ExeFilePath)
    {
        BinaryType bt;
        bool res = Native.GetBinaryType(ExeFilePath, out bt);

        if (res == false) MessageBox.Show("Not executable");
        else
        {
            switch (bt)
            {
                case BinaryType.SCS_32BIT_BINARY: MessageBox.Show("32-bit executable"); break;
                case BinaryType.SCS_64BIT_BINARY: MessageBox.Show("64-bit executable"); break;
                default: MessageBox.Show("Other executable"); break;
            }
        }
    }              

}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.