2008. március 22., szombat

Extracting Version Information


Problem/Question/Abstract:

How can I display some fields from my application's version information?

Answer:

I provide you the object that could be used for extracting version information from executables and libraries.

unit siverinfo;

interface
uses Windows, Classes, SysUtils;

type
  TVersionInfo = class(TObject)
  private
    FData: Pointer;
    FSize: Cardinal;
    FCompanyName: string;
    FFileDescription: string;
    FFileVersion: string;
    FInternalName: string;
    FLegalCopyright: string;
    FLegalTrademarks: string;
    FOriginalFilename: string;
    FProductName: string;
    FProductVersion: string;
    FComments: string;
  public
    constructor Create(FileName: string);
    destructor Destroy; override;
    property CompanyName: string read FCompanyName;
    property FileDescription: string read FFileDescription;
    property FileVersion: string read FFileVersion;
    property InternalName: string read FInternalName;
    property LegalCopyright: string read FLegalCopyright;
    property LegalTrademarks: string read FLegalTrademarks;
    property OriginalFilename: string read FOriginalFilename;
    property ProductName: string read FProductName;
    property ProductVersion: string read FProductVersion;
    property Comments: string read FComments;
  end;

implementation
{ TVersionInfo }

constructor TVersionInfo.Create(FileName: string);
var
  sz, lpHandle, tbl: Cardinal;
  lpBuffer: Pointer;
  str: PChar;
  strtbl: string;
  int: PInteger;
  hiW, loW: Word;
begin
  inherited Create;
  FSize := GetFileVersionInfoSize(PChar(FileName), lpHandle);
  FData := AllocMem(FSize);
  GetFileVersionInfo(PChar(FileName), lpHandle, FSize, FData);

  VerQueryValue(FData, '\\VarFileInfo\Translation', lpBuffer, sz);
  int := lpBuffer;
  hiW := HiWord(int^);
  loW := LoWord(int^);
  tbl := (loW shl 16) or hiW;
  strtbl := Format('%x', [tbl]);
  if Length(strtbl) < 8 then
    strtbl := '0' + strtbl;

  VerQueryValue(FData, PChar('\\StringFileInfo\' + strtbl + '\CompanyName'), lpBuffer,
    sz);
  str := lpBuffer;
  FCompanyName := str;

  VerQueryValue(FData, PChar('\\StringFileInfo\' + strtbl + '\FileDescription'),
    lpBuffer, sz);
  str := lpBuffer;
  FFileDescription := str;

  VerQueryValue(FData, PChar('\\StringFileInfo\' + strtbl + '\FileVersion'), lpBuffer,
    sz);
  str := lpBuffer;
  FFileVersion := str;

  VerQueryValue(FData, PChar('\\StringFileInfo\' + strtbl + '\InternalName'),
    lpBuffer, sz);
  str := lpBuffer;
  FInternalName := str;

  VerQueryValue(FData, PChar('\\StringFileInfo\' + strtbl + '\LegalCopyright'),
    lpBuffer, sz);
  str := lpBuffer;
  FLegalCopyright := str;

  VerQueryValue(FData, PChar('\\StringFileInfo\' + strtbl + '\LegalTrademarks'),
    lpBuffer, sz);
  str := lpBuffer;
  FLegalTrademarks := str;

  VerQueryValue(FData, PChar('\\StringFileInfo\' + strtbl + '\OriginalFilename'),
    lpBuffer, sz);
  str := lpBuffer;
  FOriginalFilename := str;

  VerQueryValue(FData, PChar('\\StringFileInfo\' + strtbl + '\ProductName'), lpBuffer,
    sz);
  str := lpBuffer;
  FProductName := str;

  VerQueryValue(FData, PChar('\\StringFileInfo\' + strtbl + '\ProductVersion'),
    lpBuffer, sz);
  str := lpBuffer;
  FProductVersion := str;

  VerQueryValue(FData, PChar('\\StringFileInfo\' + strtbl + '\Comments'), lpBuffer,
    sz);
  str := lpBuffer;
  FComments := str;
end;

destructor TVersionInfo.Destroy;
begin
  FreeMem(FData);
  inherited;
end;

end.

Nincsenek megjegyzések:

Megjegyzés küldése