function IsExecutable32Bit(
const lpExeFilename:
String): Boolean;
const
kb32 = 1024 * 32;
var
Buffer :
Array[0..kb32-1]
of Byte;
// warning: assuming both headers are in there!
hFile : DWord;
bRead : DWord;
bToRead : DWord;
pDos : PImageDosHeader;
pNt : PImageNtHeaders;
begin
Result := False;
hFile := CreateFile(pChar(lpExeFilename), GENERIC_READ, FILE_SHARE_READ,
NIL,
OPEN_EXISTING, 0, 0);
if hFile <> INVALID_HANDLE_VALUE
then
try
bToRead := GetFileSize(hFile,
NIL);
if bToRead > kb32
then bToRead := kb32;
if not ReadFile(hFile, Buffer, bToRead, bRead,
NIL)
then Exit;
if bRead = bToRead
then
begin
pDos := @Buffer[0];
if pDos.e_magic = IMAGE_DOS_SIGNATURE
then
begin
pNt := PImageNtHeaders(LongInt(pDos) + pDos._lfanew);
if pNt.Signature = IMAGE_NT_SIGNATURE
then
Result := pNt.FileHeader.Machine
and IMAGE_FILE_32BIT_MACHINE > 0;
end;
{
else
raise Exception.Create('File is not a valid executable.');
}
end;
{
else
raise Exception.Create('File is not an executable.');
}
finally
CloseHandle(hFile);
end;
end;
function IsExecutable64Bit(
const lpExeFilename:
String): Boolean;
// since as of now (march 2012), there only exist 32 and 64 bit executables,
// if its not the one, its assumably the other
begin
Result :=
not IsExecutable32Bit(lpExeFilename);
end;