Sign in Join
Wiki copytemporary

Show Application Title, Version, and Company: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Show Application Title, Version, and Company

OldNewLine
1+## Overview
2+
3+### Windows
4+
5+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.
6+
7+### Linux
8+
9+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.
10+
11+### macOS and iOS
12+
13+On macOS, Lazarus file version information can be stored in two locations:
14+
15+- the MACH-O executable (like on Windows, Linux); see below
16+- the application bundle (if present) in a plist. See [[Mac Show Application Title, Version, and Company]] for details.
17+
18+## Implementations
19+
20+### FPC 3.0+
21+
22+Implementation in FPC 3.0.x using [[fcl-res]]: see [[User Changes 3.0|announcement in User Changes 3.0]].
23+
24+The code below shows how to get executable info out of:
25+
26+- .exe/.dll/.ocx files (Windows format)
27+- Lazarus-compiled ELF executables (Linux)
28+- Lazarus-compiled MACH-O executables (macOS)
29+
30+``` pascal
31+program printfileinfo;
32+
33+{
34+ Displays file version info for
35+- Windows PE executables
36+- Linux ELF executables (compiled by Lazarus)
37+- macOS MACH-O executables (compiled by Lazarus)
38+ Runs on Windows, Linux, macOS
39+}
40+
41+{$mode objfpc}{$H+}
42+{$ifdef mswindows}{$apptype console}{$endif}
43+uses
44+ {$IFDEF UNIX}{$IFDEF UseCThreads}
45+ cthreads,
46+ {$ENDIF}{$ENDIF}
47+ Classes,sysutils
48+ // FPC 3.0 fileinfo reads exe resources as long as you register the appropriate units
49+ , fileinfo
50+ , winpeimagereader {need this for reading exe info}
51+ , elfreader {needed for reading ELF executables}
52+ , machoreader {needed for reading MACH-O executables}
53+ ;
54+
55+var
56+ FileVerInfo: TFileVersionInfo;
57+
58+{$R *.res}
59+
60+begin
61+ FileVerInfo:=TFileVersionInfo.Create(nil);
62+ try
63+ FileVerInfo.ReadFileInfo;
64+ writeln('Company: ',FileVerInfo.VersionStrings.Values['CompanyName']);
65+ writeln('File description: ',FileVerInfo.VersionStrings.Values['FileDescription']);
66+ writeln('File version: ',FileVerInfo.VersionStrings.Values['FileVersion']);
67+ writeln('Internal name: ',FileVerInfo.VersionStrings.Values['InternalName']);
68+ writeln('Legal copyright: ',FileVerInfo.VersionStrings.Values['LegalCopyright']);
69+ writeln('Original filename: ',FileVerInfo.VersionStrings.Values['OriginalFilename']);
70+ writeln('Product name: ',FileVerInfo.VersionStrings.Values['ProductName']);
71+ writeln('Product version: ',FileVerInfo.VersionStrings.Values['ProductVersion']);
72+ finally
73+ FileVerInfo.Free;
74+ end;
75+end.
76+```
77+
78+### Implementations using legacy FPC 2.6.x
79+
80+Uses [[fcl-res]]; contribution via the mailing list: [1](http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20100723/8db6b97e/attachment.ksh)
81+
82+Use this like
83+
84+``` pascal
85+uses
86+ resource, versiontypes, versionresource;
87+
88+ FUNCTION resourceVersionInfo: STRING;
89+
90+ (* Unlike most of AboutText (below), this takes significant activity at run- *)
91+ (* time to extract version/release/build numbers from resource information *)
92+ (* appended to the binary. *)
93+
94+ VAR Stream: TResourceStream;
95+ vr: TVersionResource;
96+ fi: TVersionFixedInfo;
97+
98+ BEGIN
99+ RESULT:= '';
100+ TRY
101+
102+ (* This raises an exception if version info has not been incorporated into the *)
103+ (* binary (Lazarus Project -> Project Options -> Version Info -> Version *)
104+ (* numbering). *)
105+
106+ Stream:= TResourceStream.CreateFromID(HINSTANCE, 1, PChar(RT_VERSION));
107+ TRY
108+ vr:= TVersionResource.Create;
109+ TRY
110+ vr.SetCustomRawDataStream(Stream);
111+ fi:= vr.FixedInfo;
112+ RESULT := 'Version ' + IntToStr(fi.FileVersion[0]) + '.' + IntToStr(fi.FileVersion[1]) +
113+ ' release ' + IntToStr(fi.FileVersion[2]) + ' build ' + IntToStr(fi.FileVersion[3]) + eol;
114+ vr.SetCustomRawDataStream(nil)
115+ FINALLY
116+ vr.Free
117+ END
118+ FINALLY
119+ Stream.Free
120+ END
121+ EXCEPT
122+ END
123+ END { resourceVersionInfo } ;
124+```
125+
126+Using vinfo: [2](http://forum.lazarus.freepascal.org/index.php?topic=12435.0) and [3](http://forum.lazarus.freepascal.org/index.php?topic=13957.0)
127+
128+## Related tips
129+
130+### SVN/Git/Hg/Mercurial revision
131+
132+Use \$(lazarusdir)/tools/svn2revisioninc to get revision number (from a subversion, git or mercurial repository) into a file revision.inc, e.g. something like:
133+
134+``` pascal
135+// Created by Svn2RevisionInc
136+const RevisionStr = '43594';
137+```
138+
139+## Unix-only hacks
140+
141+These work with Linux on various platforms, and probably with Solaris provided that the GNU-derived utilities are installed.
142+
143+### Getting Subversion revision information as a program-accessible string
144+
145+Put this into Project options -\> Compilation -\> Execute before -\> Command:
146+
147+`` /bin/sh -c "echo -n C`svnversion -n`C |tr A-G %-+ >project_svnrevision.inc" ``
148+
149+Note quote and backtick positions. The tr is converting C into another layer of quotes which is necessary for things to work as required.
150+
151+Put this into the program:
152+
153+``` pascal
154+ (*$IFDEF UNIX *)
155+ rev= (*$I project_svnrevision.inc *) ;
156+ (*$ELSE *)
157+ rev= 'unimplemented';
158+ (*$ENDIF *)
159+```
160+
161+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.
162+
163+### Renaming the final executable to include platform and timestamp
164+
165+Put this into Project options -\> Paths -\> Unit output directory:
166+
167+`lib/$(TargetCPU)-$(TargetOS)`
168+
169+Put this into Project options -\> Paths -\> Target file name:
170+
171+`UnyokedBackend-$(TargetCPU)-$(TargetOS)-$(LCLWidgetType)`
172+
173+Make sure that "Apply conventions" is ticked (checked). This might vary slightly according to IDE and compiler/linker versions.
174+
175+Put this into Project options -\> Compilation -\> Execute after -\> Command:
176+
177+``` bash
178+ /bin/sh -c "mv libunyokedbackend-$(TargetCPU)-$(TargetOS)-$(LCLWidgetType).so UnyokedBackend-$(TargetCPU)-$(TargetOS)-$(LCLWidgetType).`date +%F.%R`.so"
179+```
180+
181+That should be a single line. Note quote and backtick positions.
182+
183+## See also
184+
185+- [[TVersionResource]]
186+
187+---
188+*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](/wiki-contact).*
189+Category: Code
190+Category: FPC
191+Category: Lazarus
192+Category: Linux
193+Category: Windows
194+Category: macOS

Back to history