size : 6215
uploaded_on : Mon Jan 4 00:00:00 1999
modified_on : Wed Dec 8 14:03:31 1999
title : File description
org_filename : GetFileDescription.txt
author : Ulli Conrad
authoremail : uconrad@gmx.net
description : How to retrieve a description string compiled to an executable
keywords :
tested : D3
submitted_by : The CKB Crew
submitted_by_email : ckb@netalive.org
uploaded_by : nobody
modified_by : nobody
owner : nobody
lang : plain
file-type : text/plain
category : delphi-filehandling
__END_OF_HEADER__
{ this function will return a description string compiled to an executable }
type IMAGE_NEW_HEADER = record { New .EXE header }
ne_magic : Word; { Magic number NE_MAGIC }
ne_ver : Byte; { Version number }
ne_rev : Byte; { Revision number }
ne_enttab : Word; { Offset of Entry Table }
ne_cbenttab : Word; { Number of bytes in Entry Table }
ne_crc : Longint; { Checksum of whole file }
ne_flags : Word; { Flag word }
ne_autodata : Word; { Automatic data segment number }
ne_heap : Word; { Initial heap allocation }
ne_stack : Word; { Initial stack allocation }
ne_csip : Longint; { Initial CS:IP setting }
ne_sssp : Longint; { Initial SS:SP setting }
ne_cseg : Word; { Count of file segments }
ne_cmod : Word; { Entries in Module Reference Table }
ne_cbnrestab : Word; { Size of non-resident name table }
ne_segtab : Word; { Offset of Segment Table }
ne_rsrctab : Word; { Offset of Resource Table }
ne_restab : Word; { Offset of resident name table }
ne_modtab : Word; { Offset of Module Reference Table }
ne_imptab : Word; { Offset of Imported Names Table }
ne_nrestab : Longint; { Offset of Non-resident Names Table }
ne_cmovent : Word; { Count of movable ent }
ne_align : Word; { Segment alignment shift count }
ne_cres : Word; { Count of resource entries }
ne_exetyp : Byte; { Target operating system }
ne_flagsothers: Byte; { Other .EXE flags }
ne_res : array [0..7] of Byte; { Pad structure to 64 bytes }
end;
type IMAGE_DOS_HEADER = record { DOS 1, 2, 3 .EXE header }
e_magic : Word; { Magic number }
e_cblp : Word; { Words on last page of file }
e_cp : Word; { Pages in file }
e_crlc : Word; { Relocations }
e_cparhdr : Word; { Size of header in paragraphs }
e_minalloc: Word; { Minimum extra paragraphs needed }
e_maxalloc: Word; { Maximum extra paragraphs needed }
e_ss : Word; { Initial (relative) SS value }
e_sp : Word; { Initial SP value }
e_csum : Word; { Checksum }
e_ip : Word; { Initial IP value }
e_cs : Word; { Initial (relative) CS value }
e_lfarlc : Word; { File address of relocation table }
e_ovno : Word; { Overlay number }
e_res : array[0..3] of Word; { Reserved words }
e_oemid : Word; { OEM identifier (for e_oeminfo) }
e_oeminfo : Word; { OEM information; e_oemid specific }
e_res2 : array[0..9] of Word; { Reserved words }
e_lfanew : Longint; { File address of new exe header }
end;
const
IMAGE_DOS_SIGNATURE = $00005A4D; { MZ }
IMAGE_OS2_SIGNATURE = $0000454E; { NE }
IMAGE_OS2_SIGNATURE_LE = $00005A4D; { LE }
IMAGE_NT_SIGNATURE = $00004550; { PE00 }
function IsNewExe(fn : PChar;var DosHdr: IMAGE_DOS_HEADER;var NewHdr: IMAGE_NEW_HEADER): Boolean;
var
Filehandle: Integer;
BytesRead : Integer;
ofs : TOFSTRUCT;
begin
Result := False;
FillChar (ofs, sizeof (TOFSTRUCT), 0);
if OpenFile (fn, ofs, OF_EXIST or OF_READ) = -1 then
Exit;
FileHandle:=OpenFile(fn, ofs, OF_REOPEN or OF_READ);
if FileHandle=-1 then
Exit;
FillChar (DosHdr, sizeof (IMAGE_DOS_HEADER), 0);
FillChar (NewHdr, sizeof (IMAGE_NEW_HEADER), 0);
{ read MS-DOS header }
BytesRead := _lread (FileHandle, @DosHdr, sizeof (IMAGE_DOS_HEADER));
{ test for bytes read }
if BytesRead <> sizeof (IMAGE_DOS_HEADER) then
begin
_lclose (FileHandle);
Exit;
end;
{ test for magic number MZ }
if DosHdr.e_magic <> IMAGE_DOS_SIGNATURE then
begin
_lclose (FileHandle);
Exit;
end;
{ test for address of new exe header }
if DosHdr.e_lfanew <= 0 then
begin
_lclose (FileHandle);
Exit;
end;
{ fast forward to Windows header }
if _llseek (FileHandle, DosHdr.e_lfanew, 0) = -1 then
begin
_lclose (FileHandle);
Exit;
end;
{ read new exe header }
BytesRead := _lread (FileHandle, @NewHdr, sizeof (IMAGE_NEW_HEADER));
{ test for bytes read }
if BytesRead <> sizeof (IMAGE_NEW_HEADER) then
begin
_lclose (FileHandle);
Exit;
end;
{ test for signature NE }
if NewHdr.ne_magic <> IMAGE_OS2_SIGNATURE then
begin
_lclose (FileHandle);
Exit;
end;
{ passed the test }
IsNewExe := True;
_lclose (FileHandle);
end;
type
rsrc_string = record
rs_len : Byte; { number of bytes in string }
rs_string : array[0..255] of char; { text of string }
end;
function GetFileDescription(FileName : string) : string;
var
DosHdr : IMAGE_DOS_HEADER;
NewHdr : IMAGE_NEW_HEADER;
ofs : TOFSTRUCT;
FileHandle : integer;
ModuleDescription : rsrc_string;
begin
Result:='';
if IsNewExe(pChar(FileName),DosHdr,NewHdr) then
begin
FillChar (ofs, sizeof (TOFSTRUCT), 0);
if OpenFile (pChar(FileName), ofs, OF_EXIST or OF_READ) = -1 then
Exit;
FileHandle:=OpenFile (pChar(FileName), ofs, OF_REOPEN or OF_READ);
if FileHandle=-1 then
Exit;
{ goto location of non-resident name table }
_llseek (FileHandle,NewHdr.ne_nrestab, 0);
{ read length of string (in first entry of the non-resident name table) }
_lread (FileHandle, @ModuleDescription.rs_len, sizeof (Byte));
{ read module description string }
_lread (FileHandle, @ModuleDescription.rs_string, ModuleDescription.rs_len);
{ tag null termination onto string }
ModuleDescription.rs_string[ModuleDescription.rs_len] := #0;
Result:=StrPas(ModuleDescription.rs_string);
{ close file }
_lclose (FileHandle);
end;
end;