C# でコンソール アプリケーション ユーティリティを呼び出して確認しました。これは基本ですが、拡張することができます。
情報テキストをコンソールに返しますが、バッチ ファイルでこれを使用する場合に備えて、終了コードを使用してタイプを示します。
下記参照
乾杯
ロイ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ExeType
{
class Program
{
const int st_error = 99;
const int st_unknown = 98;
const int st_unidentified = 97;
const int st_not_PE_image = 96;
const int st_Exec_x86 = 1;
const int st_Exec_x64 = 2;
const int st_offset = 0x3c;
const int st_P = 0x50;
const int st_E = 0x45;
const int st_ind_x86_1 = 0x4c;
const int st_ind_x86_2 = 0x1;
const int st_ind_x64_1 = 0x64;
const int st_ind_x64_2 = 0x86;
static void Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("Please specify a file");
Environment.Exit(st_error);
}
BinaryReader br = new BinaryReader(File.OpenRead(args[0]));
byte[] block = br.ReadBytes(0x1000);
br.Close();
if (block.Length < st_offset+1)
{
Console.WriteLine("Unknown");
Environment.Exit(st_unknown);
}
int offset = (block[st_offset+1] << 8) + block[st_offset];
if (block.Length < offset)
{
Console.WriteLine("Unknown");
Environment.Exit(st_unknown);
}
int indicator1 = block[offset];
int indicator2 = block[offset+1];
if (indicator1 != st_P || indicator2 != st_E)
{
Console.WriteLine("Not a PE format image file");
Environment.Exit(st_not_PE_image);
}
offset += 4;
indicator1 = block[offset];
indicator2 = block[offset + 1];
int retval = st_unidentified;
switch (indicator1)
{
case st_ind_x86_1:
if (indicator2 == st_ind_x86_2)
{
Console.WriteLine("32 bit");
retval = st_Exec_x86;
}
break;
case st_ind_x64_1:
if (indicator2 == st_ind_x64_2)
{
Console.WriteLine("64 bit");
retval = st_Exec_x64;
}
break;
default:
Console.WriteLine("Unidentified");
break;
}
Environment.Exit( retval );
}
}
}