February 2012
» Get Version

January 2007
    Bezier Text

December 2005
    Rotated Ellipses

December 2004
    PDF Page Count

January 2003
    Boolean Blues

March 2002
    Networked Drives

January 2002
    Treeview Troubles
    Appending to Exe's



Freeware Components
Extracting an app's version number from its version resource Feb 2012
For too long I've been manually updating application version numbers in both my 'About' dialogs and in the resource section (Project | Options | Version Info). Sometimes I forget to update one so they don't match! I've finally written a very simple bit of code which extracts the version number from the resource section so my 'About' dialog version numbers always match this.

Code snippet ...

uses Windows, Classes, SysUtils;

type
  TVS_FIXEDFILEINFO = record
    dwSignature: DWORD ;             
    dwStrucVersion: DWORD ;      
    dwFileVersionMS: DWORD ;
    dwFileVersionLS: DWORD ;
    dwProductVersionMS: DWORD ;
    dwProductVersionLS: DWORD ;
    dwFileFlagsMask: DWORD ;
    dwFileFlags: DWORD ;
    dwFileOS: DWORD ;
    dwFileType: DWORD ;
    dwFileSubtype: DWORD ;
    dwFileDateMS: DWORD ;
    dwFileDateLS: DWORD ;
  end;

  TVS_VERSION_INFO = packed record
    Length          :WORD;
    wValueLength    :WORD;
    wType           :WORD;
    szKey:array[0..Length('VS_VERSION_INFO')] of WideChar;
    Padding1        :array[0..0] of Word;
    FixedInfo       :TVS_FIXEDFILEINFO;
    // WORD  Padding2[];
    // WORD  Children[];
  end;

function GetVersion: string;
var
  rs: TResourceStream;
  w: Word;
  vsvi: TVS_VERSION_INFO;
  ffi: TVS_FIXEDFILEINFO;
begin
  result := '';
  rs := TResourceStream.CreateFromID(hInstance, 1, RT_VERSION);
  try
    rs.read(vsvi, sizeof(vsvi));
    if vsvi.wValueLength <> sizeof(vsvi.FixedInfo) then exit;
    with vsvi.FixedInfo do
      result := format('%d.%d.%d (build %d)',
        [dwFileVersionMS shr 16, dwFileVersionMS and $FFFF,
        dwFileVersionLS shr 16, dwFileVersionLS and $FFFF]);
  finally
    rs.Free;
  end;
end;


//Now how easy is this? ...
lblVersionNum.caption := 'Version ' + GetVersion;

Copyright © 2002-2012 Angus Johnson