-
Notifications
You must be signed in to change notification settings - Fork 0
/
VersionInfo.pas
112 lines (101 loc) · 3.93 KB
/
VersionInfo.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
unit VersionInfo;
interface
uses Windows,
Forms,
SysUtils;
function GetVersionInfo: string;
function GetVersion: string;
function GetBuildTime: TDateTime;
implementation
//==================================================================================================
function GetVersionInfo: string;
var
VerText: PChar;
InfoSize, Wnd, VerSize: DWORD;
VerBuf: Pointer;
begin
Result := '-.-.-.-';
InfoSize := GetFileVersionInfoSize(PChar(Application.ExeName), Wnd);
if InfoSize <> 0 then begin
GetMem(VerBuf, InfoSize);
try
if GetFileVersionInfo(PChar(Application.ExeName), Wnd, InfoSize, VerBuf) then
if VerQueryValue(VerBuf, '\StringFileInfo\040904E4\FileVersion', Pointer(VerText), VerSize) then
Result := StrPas(VerText);
finally
FreeMem(VerBuf);
end;
end;
end;
//==================================================================================================
function GetBuildTime: TDateTime;
type
UShort = Word;
TImageDosHeader = packed record
e_magic: UShort; // ìàãè÷åñêîå ÷èñëî
e_cblp: UShort; // êîëè÷åñòâî áàéò íà ïîñëåäíåé ñòðàíèöå ôàéëà
e_cp: UShort; // êîëè÷åñòâî ñòðàíèö âôàéëå
e_crlc: UShort; // Relocations
e_cparhdr: UShort; // ðàçìåð çàãîëîâêà â ïàðàãðàôàõ
e_minalloc: UShort; // Minimum extra paragraphsneeded
e_maxalloc: UShort; // Maximum extra paragraphsneeded
e_ss: UShort; // íà÷àëüíîå( îòíîñèòåëüíîå ) çíà÷åíèå ðåãèñòðà SS
e_sp: UShort; // íà÷àëüíîå çíà÷åíèåðåãèñòðà SP
e_csum: UShort; // êîíòðîëüíàÿ ñóììà
e_ip: UShort; // íà÷àëüíîå çíà÷åíèå ðåãèñòðà IP
e_cs: UShort; // íà÷àëüíîå( îòíîñèòåëüíîå ) çíà÷åíèå ðåãèñòðà CS
e_lfarlc: UShort; // àäðåñ â ôàéëå íà òàáëèöó ïåðåàäðåñàöèè
e_ovno: UShort; // êîëè÷åñòâî îâåðëååâ
e_res: array[0..3] of UShort; // Çàðåçåðâèðîâàíî
e_oemid: UShort; // OEM identifier (for e_oeminfo)
e_oeminfo: UShort; // OEM information; e_oemid specific
e_res2: array[0..9] of UShort; // Çàðåçåðâèðîâàíî
e_lfanew: LongInt; // àäðåñ â ôàéëå íîâîãî .exeçàãîëîâêà
end;
TImageResourceDirectory = packed record
Characteristics: DWord;
TimeDateStamp: DWord;
MajorVersion: Word;
MinorVersion: Word;
NumberOfNamedEntries: Word;
NumberOfIdEntries: Word;
// IMAGE_RESOURCE_DIRECTORY_ENTRY DirectoryEntries[];
end;
PImageResourceDirectory = ^TImageResourceDirectory;
var
hExeFile: HFile;
ImageDosHeader: TImageDosHeader;
Signature: Cardinal;
ImageFileHeader: TImageFileHeader;
ImageOptionalHeader: TImageOptionalHeader;
ImageSectionHeader: TImageSectionHeader;
ImageResourceDirectory: TImageResourceDirectory;
Temp: Cardinal;
i: Integer;
begin
hExeFile := CreateFile(PChar(ParamStr(0)), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0);
try
ReadFile(hExeFile, ImageDosHeader, SizeOf(ImageDosHeader), Temp, nil);
SetFilePointer(hExeFile, ImageDosHeader.e_lfanew, nil, FILE_BEGIN);
ReadFile(hExeFile, Signature, SizeOf(Signature), Temp, nil);
ReadFile(hExeFile, ImageFileHeader, SizeOf(ImageFileHeader), Temp, nil);
ReadFile(hExeFile, ImageOptionalHeader, SizeOf(ImageOptionalHeader), Temp, nil);
for i := 0 to ImageFileHeader.NumberOfSections - 1 do
begin
ReadFile(hExeFile, ImageSectionHeader, SizeOf(ImageSectionHeader), Temp, nil);
if StrComp(@ImageSectionHeader.Name, '.rsrc') = 0 then Break;
end;
SetFilePointer(hExeFile, ImageSectionHeader.PointerToRawData, nil, FILE_BEGIN);
ReadFile(hExeFile, ImageResourceDirectory, SizeOf(ImageResourceDirectory), Temp, nil);
finally
FileClose(hExeFile);
end;
Result := FileDateToDateTime(ImageResourceDirectory.TimeDateStamp);
end;
//==================================================================================================
function GetVersion: string;
begin
Result := 'v ' + GetVersionInfo + #13#10'build date ' + DateToStr(GetBuildTime);
end;
//==================================================================================================
end.