Author: William Gerbert
Determine the type of an EXE file
Answer:
Here's a function to return the platform the executable was designed for (16/32 bit
Windows or DOS). Read the comment for the usage. This function works as well with
DLLs, COMs, and maybe others. Thanks to Peter Below for this code.
Use it as shown at the bottom of the code snippet.
1
2 //-------------------------------------------------------------------------
3 // function to return the type of executable or dll (DOS, 16-bit, 32-bit).
4 //-------------------------------------------------------------------------
5
6 type
7 TExeType = (etUnknown, etDOS, etWinNE {16-bit}, etWinPE {32-bit});
8
9 function GetExeType(const FileName: string): TExeType;
10 var
11 Signature,
12 WinHdrOffset: Word;
13 fexe: TFileStream;
14 begin
15 Result := etUnknown;
16 try
17 fexe := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone);
18 try
19 fexe.ReadBuffer(Signature, SizeOf(Signature));
20 if Signature = $5A4D { 'MZ' } then
21 begin
22 Result := etDOS;
23 fexe.Seek($18, soFromBeginning);
24 fexe.ReadBuffer(WinHdrOffset, SizeOf(WinHdrOffset));
25 if WinHdrOffset >= $40 then
26 begin
27 fexe.Seek($3C, soFromBeginning);
28 fexe.ReadBuffer(WinHdrOffset, SizeOf(WinHdrOffset));
29 fexe.Seek(WinHdrOffset, soFrombeginning);
30 fexe.ReadBuffer(Signature, SizeOf(Signature));
31 if Signature = $454E { 'NE' } then
32 Result := etWinNE
33 else if Signature = $4550 { 'PE' } then
34 Result := etWinPE;
35 end;
36 end;
37 finally
38 fexe.Free;
39 end;
40 except
41 end;
42 end;
43
44 begin
45 case GetExeType(aFileName) of
46 etUnknown: Label3.Caption := 'Unknown file type';
47 etDOS: Label3.Caption := 'DOS executable';
48 etWinNE: {16-bit} Label3.Caption := 'Windows 16-bit executable';
49 etWinPE: {32-bit} Label3.Caption := 'Windows 32-bit executable';
50 end;
51 end;
|