function IsExecutable32Bit(
const Filename:
String): Boolean;
var
hFile : THandle;
bRead :
{$IF Defined(NativeUInt)}NativeUInt
{$ELSE}LongWord
{$IFEND};
Buffer :
array[0..1024*64-1]
of Byte;
// Warning: Assuming both headers are in there!
DosHeader : TImageDosHeader
absolute Buffer;
NtHeader : PImageNtHeaders;
begin
Result := False;
hFile := CreateFile(PChar(Filename), GENERIC_READ, FILE_SHARE_READ,
NIL, OPEN_EXISTING, 0, 0);
if hFile <> INVALID_HANDLE_VALUE
then
begin
try
if ReadFile(hFile, Buffer, SizeOf(Buffer), bRead,
NIL)
and (DosHeader.e_magic = IMAGE_DOS_SIGNATURE)
and (DosHeader._lfanew + SizeOf(TImageNtHeaders) <= bRead)
then
begin
NtHeader := PImageNtHeaders(@Buffer[DosHeader._lfanew]);
if NtHeader.Signature = IMAGE_NT_SIGNATURE
then
Result := NtHeader.FileHeader.Machine
and IMAGE_FILE_32BIT_MACHINE <> 0;
end;
{
else
raise Exception.Create('File is not a valid executable.'); }
finally
CloseHandle(hFile);
end;
end;
{
else
raise Exception.Create('File is not readable.'); }
end;