DelphiFAQ Home Search:

Determine the type of an EXE File

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

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.

//-------------------------------------------------------------------------
//  function to return the type of executable or dll (DOS, 16-bit, 32-bit).  
//  Thanks to Peter Below (TeamB) for this code. 
//-------------------------------------------------------------------------

type
  TExeType = (etUnknown, etDOS, etWinNE {16-bit}, etWinPE {32-bit});

function GetExeType(const FileName: string): TExeType;
var
  Signature,
  WinHdrOffset: Word;
  fexe: TFileStream;
begin
  Result := etUnknown;
  try
    fexe := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone);
    try
      fexe.ReadBuffer(Signature, SizeOf(Signature));
      if Signature = $5A4D { 'MZ' } then
        begin
          Result := etDOS;
          fexe.Seek($18, soFromBeginning);
          fexe.ReadBuffer(WinHdrOffset, SizeOf(WinHdrOffset));
          if WinHdrOffset >= $40 then
            begin
              fexe.Seek($3C, soFromBeginning);
              fexe.ReadBuffer(WinHdrOffset, SizeOf(WinHdrOffset));
              fexe.Seek(WinHdrOffset, soFrombeginning);
              fexe.ReadBuffer(Signature, SizeOf(Signature));
              if Signature = $454E { 'NE' } then
                Result := etWinNE
              else
                if Signature = $4550 { 'PE' } then
                  Result := etWinPE;
            end;
        end;
    finally
      fexe.Free;
    end;
  except
  end;
end;

begin
  case GetExeType(aFileName) of
    etUnknown: Label3.Caption := 'Unknown file type';
    etDOS : Label3.Caption := 'DOS executable';
    etWinNE : {16-bit} Label3.Caption := 'Windows 16-bit executable';
    etWinPE : {32-bit} Label3.Caption := 'Windows 32-bit executable';
  end;
end;

You don't like the formatting? Check out SourceCoder then!

Comments:

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: