첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
0.00MB

▶ MachineType.cs

namespace TestProject
{
    /// <summary>
    /// 장치 타입
    /// </summary>
    public enum MachineType : uint
    {
        /// <summary>
        /// IMAGE_FILE_MACHINE_UNKNOWN
        /// </summary>
        IMAGE_FILE_MACHINE_UNKNOWN = 0,

        /// <summary>
        /// IMAGE_FILE_MACHINE_I386
        /// </summary>
        IMAGE_FILE_MACHINE_I386 = 0x014c,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_R3000
        /// </summary>
        IMAGE_FILE_MACHINE_R3000 = 0x0162,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_R4000
        /// </summary>
        IMAGE_FILE_MACHINE_R4000 = 0x0166,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_R10000
        /// </summary>
        IMAGE_FILE_MACHINE_R10000 = 0x0168,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_WCEMIPSV2
        /// </summary>
        IMAGE_FILE_MACHINE_WCEMIPSV2 = 0x0169,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_ALPHA
        /// </summary>
        IMAGE_FILE_MACHINE_ALPHA = 0x0184,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_SH3
        /// </summary>
        IMAGE_FILE_MACHINE_SH3 = 0x01a2,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_SH3DSP
        /// </summary>
        IMAGE_FILE_MACHINE_SH3DSP = 0x01a3,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_SH3E
        /// </summary>
        IMAGE_FILE_MACHINE_SH3E = 0x01a4,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_SH4
        /// </summary>
        IMAGE_FILE_MACHINE_SH4 = 0x01a6,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_SH5
        /// </summary>
        IMAGE_FILE_MACHINE_SH5 = 0x01a8,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_ARM
        /// </summary>
        IMAGE_FILE_MACHINE_ARM = 0x01c0,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_THUMB
        /// </summary>
        IMAGE_FILE_MACHINE_THUMB = 0x01c2,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_AM33
        /// </summary>
        IMAGE_FILE_MACHINE_AM33 = 0x01d3,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_POWERPC
        /// </summary>
        IMAGE_FILE_MACHINE_POWERPC = 0x01F0,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_POWERPCFP
        /// </summary>
        IMAGE_FILE_MACHINE_POWERPCFP = 0x01f1,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_IA64
        /// </summary>
        IMAGE_FILE_MACHINE_IA64 = 0x0200,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_MIPS16
        /// </summary>
        IMAGE_FILE_MACHINE_MIPS16 = 0x0266,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_ALPHA64
        /// </summary>
        IMAGE_FILE_MACHINE_ALPHA64 = 0x0284,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_MIPSFPU
        /// </summary>
        IMAGE_FILE_MACHINE_MIPSFPU = 0x0366,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_MIPSFPU16
        /// </summary>
        IMAGE_FILE_MACHINE_MIPSFPU16 = 0x0466,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_AXP64
        /// </summary>
        IMAGE_FILE_MACHINE_AXP64 = IMAGE_FILE_MACHINE_ALPHA64,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_TRICORE
        /// </summary>
        IMAGE_FILE_MACHINE_TRICORE = 0x0520,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_CEF
        /// </summary>
        IMAGE_FILE_MACHINE_CEF = 0x0CEF,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_EBC
        /// </summary>
        IMAGE_FILE_MACHINE_EBC = 0x0EBC,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_AMD64
        /// </summary>
        IMAGE_FILE_MACHINE_AMD64 = 0x8664,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_M32R
        /// </summary>
        IMAGE_FILE_MACHINE_M32R = 0x9041,
    
        /// <summary>
        /// IMAGE_FILE_MACHINE_CEE
        /// </summary>
        IMAGE_FILE_MACHINE_CEE = 0xC0EE
    }
}

 

728x90

 

▶ Program.cs

using System;
using System.IO;

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 프로그램 시작하기 - Main(argumentArray)

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        /// <param name="argumentArray">인자 배열</param>
        private static void Main(string[] argumentArray)
        {
            Console.WriteLine(Is64BitVersion(@"C:\Program Files\DAUM\PotPlayer\PotPlayer64.exe"));
            Console.WriteLine(Is64BitVersion(@"C:\Program Files (x86)\Bandicam\bdcam.exe"));
            Console.WriteLine(Is64BitVersion(@"C:\Program Files\Bandizip\Bandizip.exe"));
        }

        #endregion

        #region PE 파일 장치 타입 구하기 - GetPEFileMachineType(filePath)

        /// <summary>
        /// PE 파일 장치 타입 구하기
        /// </summary>
        /// <param name="filePath">파일 경로</param>
        /// <returns>PE 파일 장치 타입</returns>
        private static MachineType GetPEFileMachineType(string filePath)
        {
            FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

            BinaryReader binaryReader = new BinaryReader(fileStream);

            fileStream.Seek(0x3c, SeekOrigin.Begin);

            int peOffset = binaryReader.ReadInt32();

            fileStream.Seek(peOffset, SeekOrigin.Begin);

            uint peHead = binaryReader.ReadUInt32();

            if(peHead != 0x00004550)
            {
                throw new Exception("Can't find PE header");
            }

            MachineType machineType = (MachineType)binaryReader.ReadUInt16();

            binaryReader.Close();

            fileStream.Close();

            return machineType;
        }

        #endregion
        #region 64비트 버전 여부 구하기 - Is64BitVersion(filePath)

        /// <summary>
        /// 64비트 버전 여부 구하기
        /// </summary>
        /// <param name="filePath">파일 경로</param>
        /// <returns>64비트 버전 여부</returns>
        private static bool Is64BitVersion(string filePath)
        {
            MachineType machineType = GetPEFileMachineType(filePath);

            switch(machineType)
            {
                case MachineType.IMAGE_FILE_MACHINE_AMD64 :
                case MachineType.IMAGE_FILE_MACHINE_IA64  : return true;
                case MachineType.IMAGE_FILE_MACHINE_I386  : return false;
                default                                   : return false;
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요