unit verinfo;
{ verinfo:   extracts file information from the .exe file. }
interface
  type
    TVerKey = ( viInfoSize, viCompanyName, viFileDescription, viFileVersion, viInternalName,
                viLegalCopyright, viLegalTradeMarks, viOriginalFilename, viProductName,
                viProductVersion, viComments );
  function GetVersionInformation( Key: TVerKey; Desc: boolean ): string;

implementation
  uses Windows, SysUtils, Classes, Graphics, Forms, Controls, StdCtrls,
  Buttons, ExtCtrls;
    const
      InfoNum = 10;
      InfoStr : array[0..InfoNum] of String = (
                  'InfoSize',
                  'CompanyName',
                  'FileDescription',
                  'FileVersion',
                  'InternalName',
                  'LegalCopyright',
                  'LegalTradeMarks',
                  'OriginalFilename',
                  'ProductName',
                  'ProductVersion',
                  'Comments'
      );
    VerValid: boolean = FALSE;               // set to true when information is valid
  var
    DataStr : array[0..InfoNum] of String;   // the actual data

  procedure ReadVersionInfo;
  var
    S        : string;
    DWN      : DWORD;
    i        : integer;
    Buf      : PChar;
    Value    : PChar;
  begin
    S := ParamStr(0);
    DWN := GetFileVersionInfoSize( PChar(S), DWN );
    if DWN > 0 then
    begin
      Buf := AllocMem(DWN);
      DataStr[0] := IntToStr(DWN);                               // size of the info block
      GetFileVersionInfo( PChar(S),0,DWN,Buf );                  // read all version info into Buf
      for i:=1 to InfoNum do
        if VerQueryValue( Buf, PChar('StringFileInfo\040904E4\' + InfoStr[i]), Pointer(Value), DWN ) then
          DataStr[i] := Value else DataStr[i] := '';
      FreeMem( Buf, DWN );
    end else
      for i:=0 to InfoNum do DataStr[i] := '';
    VerValid := true;
  end;

  function GetVersionInformation( Key: TVerKey; Desc: boolean ): string;
  // returns the requested version information or '' if none is available
  begin
    if not VerValid then ReadVersionInfo;
    if Desc then result := InfoStr[word(Key)] + ': ' else result := '';
    result := result + DataStr[word(Key)];
  end;

end.

