Retrieving the application’s version string

If you need to retrieve at runtime your application’s version number, you’re targeting Windows, and you don’t mind just having the major and minor numbers, then SysUtils has a handy GetFileVersion function for the task:

function GetAppVersionStr: string;
var
  Rec: LongRec;
begin
  Rec := LongRec(GetFileVersion(ParamStr(0)));
  Result := Format('%d.%d', [Rec.Hi, Rec.Lo])
end;

If you want the ‘release’ and ‘build’ numbers as well, you’ll need to drop down to the Windows API:

function GetAppVersionStr: string;
var
  Exe: string;
  Size, Handle: DWORD;
  Buffer: TBytes;
  FixedPtr: PVSFixedFileInfo;
begin
  Exe := ParamStr(0);
  Size := GetFileVersionInfoSize(PChar(Exe), Handle);
  if Size = 0 then
    RaiseLastOSError;
  SetLength(Buffer, Size);
  if not GetFileVersionInfo(PChar(Exe), Handle, Size, Buffer) then
    RaiseLastOSError;
  if not VerQueryValue(Buffer, '\', Pointer(FixedPtr), Size) then
    RaiseLastOSError;
  Result := Format('%d.%d.%d.%d',
    [LongRec(FixedPtr.dwFileVersionMS).Hi,  //major
     LongRec(FixedPtr.dwFileVersionMS).Lo,  //minor
     LongRec(FixedPtr.dwFileVersionLS).Hi,  //release
     LongRec(FixedPtr.dwFileVersionLS).Lo]) //build
end;

If targeting OS X then GetFileVersion isn’t available, so you need to drop down to the OS API again. This time the code is much simpler however:

uses Macapi.CoreFoundation;

function GetAppVersionStr: string;
var
  CFStr: CFStringRef;
  Range: CFRange;
begin
  CFStr := CFBundleGetValueForInfoDictionaryKey(
    CFBundleGetMainBundle, kCFBundleVersionKey);
  Range.location := 0;
  Range.length := CFStringGetLength(CFStr);
  SetLength(Result, Range.length);
  CFStringGetCharacters(CFStr, Range, PChar(Result));
end;

This uses Core Foundation; you could use Cocoa instead, but when an equivalent ‘Core’ API is available, it’s usually the better choice in Delphi given it can be called directly, and with code almost identical to the Objective-C samples available online and elsewhere.

Advertisements

6 thoughts on “Retrieving the application’s version string

  1. Hi Chris, is there anyway (mac wise) to get the version of a dylib ?
    I want to create a BPL / Dylib for my program and need to know what version is currently available and if a newer one is ready on my server etc.

    • “I am using the standard Delphi version settings”

      I’m a bit confused, sorry – those settings are disabled for me when I set a DLL or BPL project to target OS X.

  2. Hi, sorry yes you are right BUT it wasn’t………
    I know I set the version information for the dylib in Delphi (but I have just applied the new update and now I can’t).

    So I guess now I do not know how going to work out the version for the Dylib…..

    Regards
    Anthoni

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s