using Aurora.Api; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace VisionCommon { public static class VersionChecker { private static Logger _logger = new Logger(typeof(VersionChecker).Name); private static string _MilFilePath = "C:\\Program Files\\Matrox Imaging\\Tools\\MILConfig.exe"; private static string _CogFilePath = "C:\\Program Files\\Cognex\\VisionPro\\bin\\Cognex.VisionPro.QuickBuild.exe"; // 모든 Machine Type을 Enum으로 등록 public enum MachineType : ushort { IMAGE_FILE_MACHINE_UNKNOWN = 0x0, IMAGE_FILE_MACHINE_AM33 = 0x1d3, IMAGE_FILE_MACHINE_AMD64 = 0x8664, IMAGE_FILE_MACHINE_ARM = 0x1c0, IMAGE_FILE_MACHINE_EBC = 0xebc, IMAGE_FILE_MACHINE_I386 = 0x14c, IMAGE_FILE_MACHINE_IA64 = 0x200, IMAGE_FILE_MACHINE_M32R = 0x9041, IMAGE_FILE_MACHINE_MIPS16 = 0x266, IMAGE_FILE_MACHINE_MIPSFPU = 0x366, IMAGE_FILE_MACHINE_MIPSFPU16 = 0x466, IMAGE_FILE_MACHINE_POWERPC = 0x1f0, IMAGE_FILE_MACHINE_POWERPCFP = 0x1f1, IMAGE_FILE_MACHINE_R4000 = 0x166, IMAGE_FILE_MACHINE_SH3 = 0x1a2, IMAGE_FILE_MACHINE_SH3DSP = 0x1a3, IMAGE_FILE_MACHINE_SH4 = 0x1a6, IMAGE_FILE_MACHINE_SH5 = 0x1a8, IMAGE_FILE_MACHINE_THUMB = 0x1c2, IMAGE_FILE_MACHINE_WCEMIPSV2 = 0x169, } public static void Print(AssemblyName assemblyName) { _logger.I($"{assemblyName.Name} : v{assemblyName.Version}"); } private static string GetVersion(string filePath) { var versionInfo = FileVersionInfo.GetVersionInfo(filePath); string version = versionInfo.FileVersion; return version; } public static string GetMilVersion() { return GetVersion(_MilFilePath); } public static string GetCogVersion() { return GetVersion(_CogFilePath); } private static MachineType GetMachineTypeFromFile(string filePath) { MachineType machineType; using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) using (var binaryReader = new BinaryReader(fileStream)) { // if (binaryReader.BaseStream.Length == 0) = if file length is zero // if (binaryReader.ReadUInt16() != 0x5a4d) = if it's a valid image("MZ") if (binaryReader.BaseStream.Length == 0 || binaryReader.ReadUInt16() != 0x5a4d) { return 0x0; //file size is zero OR invalid image("MZ") } fileStream.Position = 60; // this location contains the offset for the PE header var peOffset = binaryReader.ReadUInt32(); fileStream.Position = peOffset + 4; //contains the architecture machineType = (MachineType)binaryReader.ReadUInt16(); } return machineType; } private static bool? isMachineType64(string filePath) { switch (GetMachineTypeFromFile(filePath)) { case MachineType.IMAGE_FILE_MACHINE_AMD64: // if machine type is 64 case MachineType.IMAGE_FILE_MACHINE_IA64: // if machine type is 64 return true; case MachineType.IMAGE_FILE_MACHINE_I386: // if machine type is 32 _logger.FuncE($"Current Mil/Cog type is 32bit"); return false; default: // all other types return null; } } public static string GetMilType() { return GetType(_MilFilePath); } public static string GetCogType() { return GetType(_CogFilePath); } private static string GetType(string path) { var type = isMachineType64(path); if (type == null) { return "null"; } else if (type == true) { return "64bit"; } else { return "32bit"; } } private static bool CheckMilVersion() { string version = GetVersion(_MilFilePath); string[] versions = version.Split('.'); if (versions[0] == "10" && 40 <= Int32.Parse(versions[1])) { return true; } else { _logger.FuncE($"Current MIL Version[{version}] is Incompatible"); return false; } } private static bool CheckCogVersion() { string version = GetVersion(_CogFilePath); string[] versions = version.Split('.'); if (versions[0] == "69" && 3 <= Int32.Parse(versions[1])) { return true; } else { _logger.FuncE($"Current COG Version[{version}] is Incompatible"); return false; } } public static bool CheckMilVersionAndType() { if(CheckMilVersion() && isMachineType64(_MilFilePath) == true) { return true; } else { return false; } } public static bool CheckCogVersionAndType() { if(CheckCogVersion() && isMachineType64(_CogFilePath) == true) { return true; } else { return false; } } } }