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;
  Rec: LongRec;
  Rec := LongRec(GetFileVersion(ParamStr(0)));
  Result := Format('%d.%d', [Rec.Hi, Rec.Lo])

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

function GetAppVersionStr: string;
  Exe: string;
  Size, Handle: DWORD;
  Buffer: TBytes;
  FixedPtr: PVSFixedFileInfo;
  Exe := ParamStr(0);
  Size := GetFileVersionInfoSize(PChar(Exe), Handle);
  if Size = 0 then
  SetLength(Buffer, Size);
  if not GetFileVersionInfo(PChar(Exe), Handle, Size, Buffer) then
  if not VerQueryValue(Buffer, '\', Pointer(FixedPtr), Size) then
  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

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;
  CFStr: CFStringRef;
  Range: CFRange;
  CFStr := CFBundleGetValueForInfoDictionaryKey(
    CFBundleGetMainBundle, kCFBundleVersionKey);
  Range.location := 0;
  Range.length := CFStringGetLength(CFStr);
  SetLength(Result, Range.length);
  CFStringGetCharacters(CFStr, Range, PChar(Result));

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.

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…..


Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s