Author: Jonas Bilinkevicius
Is there a quick way to include the build number created by Project/ Options/
Version Info in my About Box?
Answer:
Solve 1:
Here's a function which will read the version info:
{
1 Get Major, Minor, Release, Build for an EXE or DLL file.
2 returns true = success.
3 returns false = failure, probably info not found in the file.}
4
5 function GetFileVersion(const FileName: string; var Major, Minor, Release, Build:
6 Integer;
7 var PreRelease, Debug: Boolean; var Description: string): Boolean;
8 var
9 Zero: DWORD; {set to 0 by GetFileVersionInfoSize}
10 VersionInfoSize: DWORD;
11 PVersionData: pointer;
12 PFixedFileInfo: PVSFixedFileInfo;
13 FixedFileInfoLength: UINT;
14 FileFlags: WORD;
15 begin
16 {ask Windows how big a data buffer to allocate to hold this EXE or DLL version
17 info}
18 VersionInfoSize := GetFileVersionInfoSize(pChar(FileName), Zero);
19 {if no version info in the EXE or DLL}
20 if VersionInfoSize = 0 then
21 begin
22 result := False;
23 exit;
24 end;
25 {allocate memory needed to hold version info}
26 PVersionData := AllocMem(VersionInfoSize);
27 try
28 {load version resource out of EXE or DLL into our buffer}
29 if GetFileVersionInfo(pChar(FileName), 0, VersionInfoSize, PVersionData) = false
30 then
31 begin
32 raise Exception.Create('Can''''t get version info');
33 end;
34 {get the fixed file info portion of the resource in buffer}
35 if VerQueryValue(PVersionData, '\', pointer(PFixedFileInfo),
36 FixedFileInfoLength)
37 = false then
38 begin
39 {no fixed file info in this version resource !}
40 result := False;
41 exit;
42 end;
43 {extract the info from the the fixed file data structure}
44 Major := PFixedFileInfo^.dwFileVersionMS shr 16;
45 Minor := PFixedFileInfo^.dwFileVersionMS and $FFFF;
46 Release := PFixedFileInfo^.dwFileVersionLS shr 16;
47 Build := PFixedFileInfo^.dwFileVersionLS and $FFFF;
48 FileFlags := PFixedFileInfo^.dwFileFlags;
49 PreRelease := (VS_FF_PRERELEASE and FileFlags) < > 0;
50 Debug := (VS_FF_DEBUG and FileFlags) < > 0;
51 Description := Format('Ver %d.%d, Release %d Build %d', [Major, Minor, Release,
52 Build]);
53 if PreRelease then
54 begin
55 Description := Description + ' Beta';
56 end;
57 if Debug then
58 begin
59 Description := Description + ' Debug';
60 end;
61 finally
62 FreeMem(PVersionData);
63 end;
64 result := True;
65 end;
Solve 2:
66 { ... }
67
68 TVer = record
69 case integer of
70 0: (both: integer);
71 1: (LowPart, HighPart: Word);
72 end;
73
74 TVersion = record
75 Major: Word;
76 Minor: Word;
77 Release: Word;
78 Build: Word;
79 end;
80
81 function GetVersionInfo(FileName: string): TVersion;
82 var
83 Len: DWord;
84 Size: DWord;
85 buf: pChar;
86 Info: pVSFixedFileInfo;
87 begin
88 fillChar(result, sizeOf(TVersion), #0);
89 Size := GetFileVersionInfoSize(pChar(FileName), Len);
90 if Size = 0 then
91 exit;
92 GetMem(buf, Size);
93 try
94 GetFileVersionInfo(pChar(FileName), 0, Size, buf);
95 if VerQueryValue(buf, '\', pointer(Info), Len) then
96 with Result do
97 begin
98 Major := TVer(Info.dwFileVersionMS).HighPart;
99 Minor := TVer(Info.dwFileVersionMS).LowPart;
100 Release := TVer(Info.dwFileVersionLS).HighPart;
101 Build := TVer(Info.dwFileVersionLS).LowPart;
102 end;
103 finally
104 FreeMem(buf);
105 end;
106 end;
Solve 3:
Get the version info stored inside a DLL.
107 function dgGetBuildInfo: string;
108 var
109 V1, V2, V3, V4: Word;
110 VerInfoSize: DWORD;
111 VerInfo: Pointer;
112 VerValueSize: DWORD;
113 VerValue: PVSFixedFileInfo;
114 Dummy: DWORD;
115 begin
116 VerInfoSize := GetFileVersionInfoSize(PChar(ParamStr(0)), Dummy);
117 GetMem(VerInfo, VerInfoSize);
118 try
119 GetFileVersionInfo(PChar(ParamStr(0)), 0, VerInfoSize, VerInfo);
120 VerQueryValue(VerInfo, '\', Pointer(VerValue), VerValueSize);
121 with VerValue^ do
122 begin
123 V1 := dwFileVersionMS shr 16;
124 V2 := dwFileVersionMS and $FFFF;
125 V3 := dwFileVersionLS shr 16;
126 V4 := dwFileVersionLS and $FFFF;
127 end;
128 Result := IntToStr(V1) + '.' + IntToStr(V2) + '.' + IntToStr(V3) + '.' +
129 IntToStr(V4);
130 finally
131 FreeMem(VerInfo, VerInfoSize);
132 end;
133 end;
|