whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1548)

Database (90)
Files (137)
Forms (107)
Graphic (116)
IDE (21)
Indy (5)
Internet / LAN (131)
IntraWeb (0)
Math (76)
Misc (127)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (268)
VCL (243)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

13 Visitors Online


 
...define binary exe type?
Autor: Simon Grossenbacher
Homepage: http://www.swissdelphicenter.ch
[ Print tip ]  

Tip Rating (7):  
     


{1.}

function GetEXEType(FileName: string): string;
var
  
BinaryType: DWORD;
begin
  if 
GetBinaryType(PChar(FileName), Binarytype) then
    case 
BinaryType of
      
SCS_32BIT_BINARY: Result := 'Win32 executable';
      SCS_DOS_BINARY: Result   := 'DOS executable';
      SCS_WOW_BINARY: Result   := 'Win16 executable';
      SCS_PIF_BINARY: Result   := 'PIF file';
      SCS_POSIX_BINARY: Result := 'POSIX executable';
      SCS_OS216_BINARY: Result := 'OS/2 16 bit executable'
        else
          
Result := 'unknown executable'
    end
  else
    
Result := 'File is not an executable';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  
label1.Caption := GetEXEType('c:\windows\notepad.exe');
end;


{
 Windows NT/2000: Requires Windows NT 3.5 or later.
 Windows 95/98: Unsupported.
}


{********************************************************************}

{2.}

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

function GetExeType(const FileName: string): TExeType;
{ func to return the type of executable or dll (DOS, 16-bit, 32-bit). }
(**************************************************************
Usage:
  with OpenDialog1 do
    if Execute then
      begin
        Label1.Caption := FileName;
        Label2.Caption := ExeStrings[GetExetype(FileName)];
      end;

  - or -

  case GetExeType(OpenDialog1.FileName) 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;
***************************************************************)
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;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if 
OpenDialog1.Execute then
    case 
GetExeType(OpenDialog1.FileName) of
      
etUnknown: Label_ExeType.Caption := 'Unknown file type';
      etDOS    : Label_ExeType.Caption := 'DOS executable';
      etWinNE  : Label_ExeType.Caption := 'Windows 16-bit executable';
      etWinPE  : Label_ExeType.Caption := 'Windows 32-bit executable';
    end;
end;


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners