Application version number

How to get the version number of an application at runtime

unit GetVersionInfo;

interface

uses
  Windows, Classes, SysUtils;

procedure GetProjectVersionInfo(AVersionList: TStrings; AFileName: string = '');
function GetBuildInfo(var V1, V2, V3, V4: Word; AFileName: string = ''): Boolean;
function GetBuildInfoAsString(AFileName: string = ''): string;

implementation

type
  PTransBuffer = ^TTransBuffer;
  TTransBuffer = array[1..13] of smallint;

const
  CInfoStr : array[1..13] of string =
    ('FileVersion',
     'CompanyName',
     'FileDescription',
     'InternalName',
     'LegalCopyright',
     'LegalTradeMarks',
     'OriginalFileName',
     'ProductName',
     'ProductVersion',
     'Comments',
     'CurrentProgramVersion',
     'CurrentDatabaseVersion',
     'VersionDetails');

procedure GetProjectVersionInfo(AVersionList: TStrings; AFileName: string = '');
{
 This procedure returns ALL of the version information as separate
 string entries of a TString list. Each element can then be accessed
 by indexing the TString list thus: AVersionList[0], AVersionList[1] etc..
}
var
  I: Integer;
  InfoSize: DWORD;
  pTrans: PTransBuffer;
  TransStr: string;
  TypeStr: string;
  Value: PChar;
  VerBuf: pointer;
  VerSize: DWORD;
  Wnd: DWORD;
begin
  AVersionList.Clear;
  if AFileName = '' then
    AFileName := ParamStr(0);
  InfoSize := GetFileVersioninfoSize(PChar(AFileName), Wnd);

  if (InfoSize <> 0) then
  begin
    GetMem(VerBuf, InfoSize);
    try
      if GetFileVersionInfo(PChar(AFileName), Wnd, InfoSize, VerBuf) then
      begin
        VerQueryValue(VerBuf, PChar('\VarFileInfo\Translation'),
                      Pointer(pTrans), VerSize);

        TransStr := IntToHex(pTrans^[1], 4) + IntToHex(pTrans^[2], 4);

        for i := Low(CInfoStr) to High(CInfoStr) do
        begin
          TypeStr := 'StringFileInfo\' + TransStr + '\' + CInfoStr[I];

          if VerQueryvalue(VerBuf, PChar(TypeStr),
                           Pointer(Value), VerSize) then
            AVersionList.Add(CInfoStr[I] + '=' + Value);
        end
      end;
    finally
      FreeMem(VerBuf);
    end;
  end;
end;

function GetBuildInfo(var V1, V2, V3, V4: Word; AFileName: string = ''): Boolean;
{
 This procedure returns the individual Major/Minor/Release/Build
 values of the version information.
}
var
  VerInfoSize: DWORD;
  VerInfo: Pointer;
  VerValueSize: DWORD;
  VerValue: PVSFixedFileInfo;
  Dummy: DWORD;
begin
  Result := True;
  if AFileName = '' then
    AFileName := ParamStr(0);
  VerInfoSize := GetFileVersionInfoSize(PChar(AFileName), Dummy);
  if VerInfoSize = 0 then
  begin
    Result := False;
    Exit;
  end;
  GetMem(VerInfo, VerInfoSize);
  try
    GetFileVersionInfo(PChar(AFileName), 0, VerInfoSize, VerInfo);
    VerQueryValue(VerInfo, '\', Pointer(VerValue), VerValueSize);

    with VerValue^ do
    begin
      V1 := dwFileVersionMS shr 16;
      V2 := dwFileVersionMS and $FFFF;
      V3 := dwFileVersionLS shr 16;
      V4 := dwFileVersionLS and $FFFF;
    end;
  finally
    FreeMem(VerInfo, VerInfoSize);
  end;
end;

function GetBuildInfoAsString(AFileName: string = ''): string;
var
  V1: Word;
  V2: Word;
  V3: Word;
  V4: Word;
begin
  if GetBuildInfo(V1, V2, V3, V4) then
    Result := Format('%d.%d.%d.%d', [V1, V2, V3, V4])
  else
    Result := '';
end;

end.

 

Share this article!

Follow us!

Find more helpful articles: