Sign in Join
Wiki copytemporary

Show Application Title, Version, and Company

English · Русский

Overview

Windows

From its earliest versions, Microsoft Windows had the capability of storing version information etc. in an executable file. Support for that was added to Delphi around v3, and was partly functional in Lazarus 0.9.24.

Linux

There is no implicit provision in the ELF file format for version numbers, copyright and so on, but Lazarus stores this in resource sections. Reading the information back at runtime relies on FPC units, below is some useful information.

macOS and iOS

On macOS, Lazarus file version information can be stored in two locations:

Implementations

FPC 3.0+

Implementation in FPC 3.0.x using fcl-res: see announcement in User Changes 3.0.

The code below shows how to get executable info out of:

pascal
program printfileinfo;

{ 
  Displays file version info for 
- Windows PE executables 
- Linux ELF executables (compiled by Lazarus)
- macOS MACH-O executables (compiled by Lazarus)
  Runs on Windows, Linux, macOS
}

{$mode objfpc}{$H+}
{$ifdef mswindows}{$apptype console}{$endif}
uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes,sysutils
  // FPC 3.0 fileinfo reads exe resources as long as you register the appropriate units
  , fileinfo
  , winpeimagereader {need this for reading exe info}
  , elfreader {needed for reading ELF executables}
  , machoreader {needed for reading MACH-O executables}
  ;

var
  FileVerInfo: TFileVersionInfo;

{$R *.res}

begin
  FileVerInfo:=TFileVersionInfo.Create(nil);
  try
    FileVerInfo.ReadFileInfo;
    writeln('Company: ',FileVerInfo.VersionStrings.Values['CompanyName']);
    writeln('File description: ',FileVerInfo.VersionStrings.Values['FileDescription']);
    writeln('File version: ',FileVerInfo.VersionStrings.Values['FileVersion']);
    writeln('Internal name: ',FileVerInfo.VersionStrings.Values['InternalName']);
    writeln('Legal copyright: ',FileVerInfo.VersionStrings.Values['LegalCopyright']);
    writeln('Original filename: ',FileVerInfo.VersionStrings.Values['OriginalFilename']);
    writeln('Product name: ',FileVerInfo.VersionStrings.Values['ProductName']);
    writeln('Product version: ',FileVerInfo.VersionStrings.Values['ProductVersion']);
  finally
    FileVerInfo.Free;
  end;
end.

Implementations using legacy FPC 2.6.x

Uses fcl-res; contribution via the mailing list: 1

Use this like

pascal
uses
  resource, versiontypes, versionresource;

 FUNCTION resourceVersionInfo: STRING;
 
 (* Unlike most of AboutText (below), this takes significant activity at run-    *)
 (* time to extract version/release/build numbers from resource information      *)
 (* appended to the binary.                                                      *)
 
 VAR     Stream: TResourceStream;
         vr: TVersionResource;
         fi: TVersionFixedInfo;
 
 BEGIN
   RESULT:= '';
   TRY
 
 (* This raises an exception if version info has not been incorporated into the  *)
 (* binary (Lazarus Project -> Project Options -> Version Info -> Version        *)
 (* numbering).                                                                  *)
 
     Stream:= TResourceStream.CreateFromID(HINSTANCE, 1, PChar(RT_VERSION));
     TRY
       vr:= TVersionResource.Create;
       TRY
         vr.SetCustomRawDataStream(Stream);
         fi:= vr.FixedInfo;
         RESULT := 'Version ' + IntToStr(fi.FileVersion[0]) + '.' + IntToStr(fi.FileVersion[1]) +
                ' release ' + IntToStr(fi.FileVersion[2]) + ' build ' + IntToStr(fi.FileVersion[3]) + eol;
         vr.SetCustomRawDataStream(nil)
       FINALLY
         vr.Free
       END
     FINALLY
       Stream.Free
     END
   EXCEPT
   END
 END { resourceVersionInfo } ;

Using vinfo: 2 and 3

Related tips

SVN/Git/Hg/Mercurial revision

Use $(lazarusdir)/tools/svn2revisioninc to get revision number (from a subversion, git or mercurial repository) into a file revision.inc, e.g. something like:

pascal
// Created by Svn2RevisionInc
const RevisionStr = '43594';

Unix-only hacks

These work with Linux on various platforms, and probably with Solaris provided that the GNU-derived utilities are installed.

Getting Subversion revision information as a program-accessible string

Put this into Project options -> Compilation -> Execute before -> Command:

text
/bin/sh -c "echo -n C`svnversion -n`C |tr A-G %-+ >project_svnrevision.inc"

Note quote and backtick positions. The tr is converting C into another layer of quotes which is necessary for things to work as required.

Put this into the program:

pascal
   (*$IFDEF UNIX   *)
           rev= (*$I project_svnrevision.inc *) ;
   (*$ELSE         *)
           rev= 'unimplemented';
   (*$ENDIF        *)

Note that that has to be a string, since the revision number will have a non-numeric suffix if the project has been updated since it was last committed.

Renaming the final executable to include platform and timestamp

Put this into Project options -> Paths -> Unit output directory:

text
lib/$(TargetCPU)-$(TargetOS)

Put this into Project options -> Paths -> Target file name:

text
UnyokedBackend-$(TargetCPU)-$(TargetOS)-$(LCLWidgetType)

Make sure that "Apply conventions" is ticked (checked). This might vary slightly according to IDE and compiler/linker versions.

Put this into Project options -> Compilation -> Execute after -> Command:

bash
 /bin/sh -c "mv libunyokedbackend-$(TargetCPU)-$(TargetOS)-$(LCLWidgetType).so UnyokedBackend-$(TargetCPU)-$(TargetOS)-$(LCLWidgetType).`date +%F.%R`.so"

That should be a single line. Note quote and backtick positions.

See also


From the Free Pascal wiki, page "Show Application Title, Version, and Company", as it stood on 2020-08-22 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 2, last changed by Trev .

Edited on wiki.freepascal.org by 11 people, 2013–2020

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
MarkMLlAttempt at initial version.
BigChimpcorrected fcl-res being in fpc trunk; layout
BigChimpclarify article not only about elf (or mach or winpe)
BigChimpFPC 2.7.1/trunk: sample program
BigChimpFPC 2.7.1/trunk (minor)
BigChimpFPC 2.7.1/trunk: Works on Linux, too. probably osx as well...
BigChimpadded cross platform revision tool
JwdietrichStructure improved.
BigChimpput back fcl-res; layout
BigChimpSorry, DoDi, you're wrong. There is version info in Laz OSX executables.
JonasMac OS X and iOS: OSX -> OS X (minor)
BigChimpWindows (minor)
BigChimpFPC 2.7.1/trunk
CCRDudeFPC 2.7.1/trunk: Filename is in ParamStr(0), NOT ParamStr(1)
WpImplementations using FPC 2.6.x: Add valid uses clause
NouziImplementations (minor)
NouziFPC 2.7.1/trunk (minor)
ThaddyFPC 3.0
ThaddyFPC 3.0
Thaddychanged order: current first
ThaddyFPC 3.0
ThaddyFPC 3.0
ThaddyFPC 3.0 adapted example code
ThaddyFPC 3.0
ThaddyFPC 3.0
ThaddyFPC 3.0
ThaddyFPC 3.0
ThaddyFPC 3.0 (minor)
MathiasFPC 3.0
TrevFixed syntax highlighting (minor)
TrevRationalised macOS categories (minor)
TrevmacOS and iOS: OSX -> macOS (minor)
Zoltanleo
TrevFPC 3.0+: OSX -> macOS (minor)
TrevAdd see also section

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.