Here's a C++ MFC console application that writes out the file header information. You can check the machine type (IMAGE_FILE_HEADER Machine member) or the IMAGE_FILE_32BIT_MACHINE flag in the Characteristics to see what platform the file is built for. See WinNT.h for more info on the structures.
#include "stdafx.h"
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
int nrd;
IMAGE_DOS_HEADER idh;
IMAGE_NT_HEADERS inth;
IMAGE_FILE_HEADER ifh;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
return 1;
}
if (argc != 2) {
_ftprintf(stderr, _T("Usage: %s filename\n"), argv[0]);
return 1;
}
// Try to open the file
CFile ckf;
CFileException ex;
DWORD flags = CFile::modeRead | CFile::shareDenyNone;
if (!ckf.Open(argv[1], flags, &ex)) {
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
_tprintf_s(_T("Couldn't open file: %1024s"), szError);
return 2;
}
// The following is adapted from:
// https://stackoverflow.com/questions/495244/how-can-i-test-a-windows-dll-file-to-determine-if-it-is-32-bit-or-64-bit
// https://stackoverflow.com/questions/46024914/how-to-parse-exe-file-and-get-data-from-image-dos-header-structure-using-c-and
// Seek to beginning of file
ckf.Seek(0, CFile::begin);
// Read DOS header
int nbytes = sizeof(IMAGE_DOS_HEADER);
nrd = ckf.Read(&idh, nbytes);
// The idh.e_lfanew member is the offset to the NT_HEADERS structure
ckf.Seek(idh.e_lfanew, CFile::begin);
// Read NT headers
nbytes = sizeof(IMAGE_NT_HEADERS);
nrd = ckf.Read(&inth, nbytes);
ifh = inth.FileHeader;
_ftprintf(stdout, _T("File machine type: "));
switch (ifh.Machine) {
case IMAGE_FILE_MACHINE_I386:
_ftprintf(stdout, _T("I386\n"));
break;
case IMAGE_FILE_MACHINE_IA64:
_ftprintf(stdout, _T("IA64\n"));
break;
case IMAGE_FILE_MACHINE_AMD64:
_ftprintf(stdout, _T("AMD64\n"));
break;
default:
_ftprintf(stdout, _T("Unknown (%d = %X)\n"), ifh.Machine, ifh.Machine);
break;
}
// Write characteristics (see WinNT.h)
_ftprintf(stdout, _T("Characteristics:\n"));
_ftprintf(stdout, _T("RELOCS_STRIPPED Relocation info stripped from file: %c\n"),
(ifh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED ? _T('Y') : _T('N')));
_ftprintf(stdout, _T("EXECUTABLE_IMAGE File is executable (i.e. no unresolved externel references): %c\n"),
(ifh.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE ? _T('Y') : _T('N')));
_ftprintf(stdout, _T("LINE_NUMS_STRIPPED Line nunbers stripped from file: %c\n"),
(ifh.Characteristics & IMAGE_FILE_LINE_NUMS_STRIPPED ? _T('Y') : _T('N')));
_ftprintf(stdout, _T("LOCAL_SYMS_STRIPPED Local symbols stripped from file: %c\n"),
(ifh.Characteristics & IMAGE_FILE_LOCAL_SYMS_STRIPPED ? _T('Y') : _T('N')));
_ftprintf(stdout, _T("AGGRESIVE_WS_TRIM Agressively trim working set: %c\n"),
(ifh.Characteristics & IMAGE_FILE_AGGRESIVE_WS_TRIM ? _T('Y') : _T('N')));
_ftprintf(stdout, _T("LARGE_ADDRESS_AWARE App can handle >2gb addresses: %c\n"),
(ifh.Characteristics & IMAGE_FILE_LARGE_ADDRESS_AWARE ? _T('Y') : _T('N')));
_ftprintf(stdout, _T("BYTES_REVERSED_LO Bytes of machine word are reversed: %c\n"),
(ifh.Characteristics & IMAGE_FILE_BYTES_REVERSED_LO ? _T('Y') : _T('N')));
_ftprintf(stdout, _T("32BIT_MACHINE 32 bit word machine: %c\n"),
(ifh.Characteristics & IMAGE_FILE_32BIT_MACHINE ? _T('Y') : _T('N')));
_ftprintf(stdout, _T("DEBUG_STRIPPED Debugging info stripped from file in .DBG file: %c\n"),
(ifh.Characteristics & IMAGE_FILE_DEBUG_STRIPPED ? _T('Y') : _T('N')));
_ftprintf(stdout, _T("REMOVABLE_RUN_FROM_SWAP If Image is on removable media, copy and run from the swap file: %c\n"),
(ifh.Characteristics & IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP ? _T('Y') : _T('N')));
_ftprintf(stdout, _T("NET_RUN_FROM_SWAP If Image is on Net, copy and run from the swap file: %c\n"),
(ifh.Characteristics & IMAGE_FILE_NET_RUN_FROM_SWAP ? _T('Y') : _T('N')));
_ftprintf(stdout, _T("SYSTEM System File: %c\n"),
(ifh.Characteristics & IMAGE_FILE_SYSTEM ? _T('Y') : _T('N')));
_ftprintf(stdout, _T("DLL File is a DLL: %c\n"),
(ifh.Characteristics & IMAGE_FILE_DLL ? _T('Y') : _T('N')));
_ftprintf(stdout, _T("UP_SYSTEM_ONLY File should only be run on a UP machine: %c\n"),
(ifh.Characteristics & IMAGE_FILE_UP_SYSTEM_ONLY ? _T('Y') : _T('N')));
_ftprintf(stdout, _T("BYTES_REVERSED_HI Bytes of machine word are reversed: %c\n"),
(ifh.Characteristics & IMAGE_FILE_BYTES_REVERSED_HI ? _T('Y') : _T('N')));
ckf.Close();
return nRetCode;
}