-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexeinfo.pas
547 lines (480 loc) · 16.7 KB
/
exeinfo.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
unit ExeInfo;
interface
uses
Windows, Messages, SysUtils, Classes, Forms, Graphics, Controls, Dialogs,
TypInfo;
const
MAJ_VER = 1; // Major version nr.
MIN_VER = 3; // Minor version nr.
REL_VER = 3; // Release nr.
BLD_VER = 0; // Build nr.
type
[ComponentPlatformsAttribute(pidWin32 or pidWin64)]
TExeInfo = class(TComponent)
private
{ Private declarations }
FCompanyName : String;
FFileDescription : String;
FFileVersion : String;
FInternalName : String;
FLegalCopyright : String;
FLegalTradeMark : String;
FOriginalFileName : String;
FProductName : String;
FProductVersion : String;
FComments : String;
FComputerName : String;
FOsName : String;
FWindowsDir : String;
FSystemDir : String;
FTempDir : String;
FFileFlags : integer;
FFileOS : integer;
FFileType : integer;
FFileCreation : TDateTime;
FMyDocumentsDir: string;
function GetVersion: string;
procedure SetVersion(const Value: string);
function GetFileVersionInt: integer;
function GetMyDocumentsDir: string;
protected
{ Protected declarations }
function GetVersionNr: Integer; virtual;
procedure GetVersionInfo; virtual;
function GetComputerName : String; virtual;
function GetWinDir : String;
function GetSysDir : String;
function GetTempDir : String;
function GetVersionPart(idx: integer): integer;
public
{ Public declarations }
procedure GetVersionInfoOfApp(const AAppName:String);
constructor Create(AOwner: TComponent); override;
property FileFlags : integer read FFileFlags;
property FileOS : integer read FFileOS;
property FileType : integer read FFileType;
property FileCreation : TDateTime read FFileCreation;
function GetOperatingSystem : string; virtual;
function MajorVersion: integer;
function MinorVersion: integer;
function ReleaseNumber: integer;
function BuildNumber: integer;
property FileVersionInt: integer read GetFileVersionInt;
published
{ Published declarations }
property CompanyName : string read FCompanyName write FCompanyName stored false;
property FileDescription : string read FFileDescription write FFileDescription stored false;
property FileVersion : string read FFileVersion write FFileVersion stored false;
property InternalName : string read FInternalName write FInternalName stored false;
property LegalCopyright : string read FLegalCopyright write FLegalCopyright stored false;
property LegalTradeMark : string read FLegalTradeMark write FLegalTradeMark stored false;
property OriginalFileName : string read FOriginalFileName write FOriginalFileName stored false;
property ProductName : string read FProductName write FProductName stored false;
property ProductVersion : string read FProductVersion write FProductVersion stored false;
property Comments : string read FComments write FComments stored false;
property ComputerName : string read GetComputerName stored false;
property OSName : string read GetOperatingSystem write FOSName stored false;
property WindowsDir : string read GetWinDir write FWindowsDir stored false;
property SystemDir : string read GetSysDir write FSystemDir stored false;
property TempDir : string read GetTempDir write FTempDir stored false;
property MyDocumentsDir : string read GetMyDocumentsDir write FMyDocumentsDir stored false;
property Version : string read GetVersion write SetVersion;
end;
implementation
uses
StrUtils, ShlObj;
function TExeInfo.BuildNumber: integer;
begin
Result := GetVersionPart(3);
end;
constructor TExeInfo.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FCompanyName := 'Updated at run-time';
FFileDescription := 'Updated at run-time';
FFileVersion := 'Updated at run-time';
FInternalName := 'Updated at run-time';
FLegalCopyright := 'Updated at run-time';
FLegalTradeMark := 'Updated at run-time';
FOriginalFileName := 'Updated at run-time';
FProductName := 'Updated at run-time';
FProductVersion := 'Updated at run-time';
FComments := 'Updated at run-time';
FComputerName := '';
if not (csDesigning in ComponentState) then
Begin
GetVersionInfo;
FComputerName := GetComputerName;
End;
end;
procedure TExeInfo.GetVersionInfoOfApp(const AAppName: string);
type
PTransBuffer = ^TTransBuffer;
TTransBuffer = array[1..4] of smallint;
var
iAppSize, iLenOfValue : DWord;
pcBuf,pcValue : PChar;
VerSize : DWord;
pTrans : PTransBuffer;
TransStr : string;
sAppName : String;
fvip : pointer;
ft : TFileTime;
st : TSystemTime;
begin
sAppName := AAppName;
// get version information values
iAppSize:= GetFileVersionInfoSize(PChar(sAppName),// pointer to filename string
iAppSize); // pointer to variable to receive zero
// if GetFileVersionInfoSize is successful
if (iAppSize > 0) then
begin
pcBuf := AllocMem(iAppSize);
GetFileVersionInfo(PChar(sAppName), // pointer to filename string
0, // ignored
iAppSize, // size of buffer
pcBuf); // pointer to buffer to receive file-version info.
VerQueryValue(pcBuf, '\', fvip, iLenOfValue);
FFileFlags := TVSFixedFileInfo(fvip^).dwFileFlags and TVSFixedFileInfo (fvip^).dwFileFlagsMask;
FFileOS := TVSFixedFileInfo(fvip^).dwFileOS;
FFileType := TVSFixedFileInfo(fvip^).dwFileType;
ft.dwLowDateTime := TVSFixedFileInfo(fvip^).dwFileDateLS;
ft.dwHighDateTime := TVSFixedFileInfo(fvip^).dwFileDateMS;
if (ft.dwLowDateTime <> 0) or (ft.dwHighDateTime <> 0) then
begin
FileTimeToSystemTime(ft,st);
FFileCreation := SystemTimeToDateTime(st);
end
else
begin
FileAge(Application.ExeName, FFileCreation);
end;
VerQueryValue(pcBuf, PChar('\VarFileInfo\Translation'),
pointer(ptrans), verSize);
TransStr:= IntToHex(ptrans^[1], 4) + IntToHex(ptrans^[2], 4);
if VerQueryValue(pcBuf,PChar('StringFileInfo\' + TransStr + '\' +
'CompanyName'), Pointer(pcValue),iLenOfValue) then
FCompanyName := pcValue
Else FCompanyName := '';
if VerQueryValue(pcBuf,PChar('StringFileInfo\' + TransStr + '\' +
'FileDescription'), Pointer(pcValue),iLenOfValue) then
FFileDescription := pcValue
Else FFileDescription := '';
if VerQueryValue(pcBuf,PChar('StringFileInfo\' + TransStr + '\' +
'FileVersion'), Pointer(pcValue),iLenOfValue) then
FFileVersion := pcValue
Else FFileVersion := '';
if VerQueryValue(pcBuf,PChar('StringFileInfo\' + TransStr + '\' +
'InternalName'), Pointer(pcValue),iLenOfValue) then
FInternalName := pcValue
Else FInternalName := '';
if VerQueryValue(pcBuf,PChar('StringFileInfo\' + TransStr + '\' +
'LegalCopyright'), Pointer(pcValue),iLenOfValue) then
FLegalCopyright := pcValue
Else FLegalCopyright := '';
if VerQueryValue(pcBuf,PChar('StringFileInfo\' + TransStr + '\' +
'LegalTradeMarks'), Pointer(pcValue),iLenOfValue) then
FLegalTradeMark := pcValue
Else FLegalTradeMark := '';
if VerQueryValue(pcBuf,PChar('StringFileInfo\' + TransStr + '\' +
'OriginalFileName'), Pointer(pcValue),iLenOfValue) then
FOriginalFileName := pcValue
Else FOriginalFileName := '';
if VerQueryValue(pcBuf,PChar('StringFileInfo\' + TransStr + '\' +
'ProductName'), Pointer(pcValue),iLenOfValue) then
FProductName := pcValue
Else FProductName := '';
if VerQueryValue(pcBuf,PChar('StringFileInfo\' + TransStr + '\' +
'ProductVersion'), Pointer(pcValue),iLenOfValue) then
FProductVersion := pcValue
Else FProductVersion := '';
if VerQueryValue(pcBuf,PChar('StringFileInfo\' + TransStr + '\' +
'Comments'), Pointer(pcValue),iLenOfValue) then
FComments := pcValue
Else FComments := '';
FreeMem(pcBuf,iAppSize);
end;
end;
procedure TExeInfo.GetVersionInfo;
begin
GetVersionInfoOfApp(Application.ExeName);
end;
function TExeInfo.GetComputerName : String;
var
pcComputer : PChar;
dwCSize : DWORD;
begin
dwCSize := MAX_COMPUTERNAME_LENGTH + 1;
GetMem( pcComputer, dwCSize * 2); // allocate memory for the string
try
if Windows.GetComputerName( pcComputer, dwCSize ) then
GetComputerName := StrPas(pcComputer);
finally
FreeMem( pcComputer ); // now free the memory allocated for the string
end;
end;
function TExeInfo.GetFileVersionInt: integer;
var
sl: TStringList;
verstr: string;
i: integer;
begin
Result := -1;
if FileVersion = '' then
Exit;
verstr := StringReplace(FileVersion,'.','.',[rfReplaceAll]);
sl := TStringList.Create;
try
sl.CommaText := verstr;
verstr := '';
for i := 0 to sl.Count - 1 do
begin
while (length(sl[i]) < 2) do
sl[i] := '0' + sl[i];
sl[i] := copy(sl[i],1,2);
verstr := verstr + sl[i];
end;
finally
sl.Free;
end;
Result := StrToInt(verstr);
end;
function TExeInfo.GetMyDocumentsDir: string;
var
r: Bool;
path: array[0..Max_Path] of Char;
begin
r := ShGetSpecialFolderPath(0, path, CSIDL_Personal, False) ;
if not r then
raise Exception.Create('Could not find MyDocuments folder location.') ;
Result := Path;
end;
function TExeInfo.GetOperatingSystem : string;
const
SM_SERVERR2 = 89;
VER_NT_WORKSTATION = $0000001;
type
pfnRtlGetVersion = function(var RTL_OSVERSIONINFOEXW): DWORD; stdcall;
type
TOSVersionInfoEx = record
dwOSVersionInfoSize:DWORD;
dwMajorVersion:DWORD;
dwMinorVersion:DWORD;
dwBuildNumber:DWORD;
dwPlatformId:DWORD;
szCSDVersion: array[0..127] of Char;
wServicePackMajor:WORD;
wServicePackMinor:WORD;
wSuiteMask:WORD;
wProductType:BYTE;
wReserved:BYTE;
End;
var
osVerInfo: TOSVersionInfoEx;
majorVer, minorVer: Cardinal;
//{$IFDEF DELPHI_UNICODE}
ver: RTL_OSVERSIONINFOEXW;
RtlGetVersion: pfnRtlGetVersion;
procedure GetUnmanistedVersion(var majv,minv: cardinal);
begin
@RtlGetVersion := GetProcAddress(GetModuleHandle('ntdll.dll'), 'RtlGetVersion');
if Assigned(RtlGetVersion) then
begin
ZeroMemory(@ver, SizeOf(ver));
ver.dwOSVersionInfoSize := SizeOf(ver);
if RtlGetVersion(ver) = 0 then
begin
majv := ver.dwMajorVersion;
minv := ver.dwMinorVersion;
end;
end;
end;
begin
Result := 'Unknown';
{ set operating system type flag }
osVerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfoEx);
if GetVersionEx(Windows.POSVersionInfo(@osVerInfo)^) then
begin
majorVer := osVerInfo.dwMajorVersion;
minorVer := osVerInfo.dwMinorVersion;
case osVerInfo.dwPlatformId of
VER_PLATFORM_WIN32_NT: { Windows NT/2000 }
begin
if majorVer <= 4 then
Result := 'Windows NT'
else if (majorVer = 5) and (minorVer = 0) then
Result := 'Windows 2000'
else if (majorVer = 5) and (minorVer = 1) then
Result := 'Windows XP'
else if (majorVer = 5) and (minorVer = 2) then
Result := 'Windows 2003'
else if (majorVer = 6) and (minorVer = 0) then
begin
Result := 'Windows Vista';
if osVerInfo.wProductType = VER_NT_WORKSTATION then
Result := 'Windows Vista'
else
Result := 'Windows Server 2008';
end
else if (majorVer = 6) and (minorVer = 1) then
begin
if osVerInfo.wProductType = VER_NT_WORKSTATION then
Result := 'Windows 7'
else
Result := 'Windows Server 2008R2';
end
else if (majorVer = 6) and (minorVer = 2) then
begin
GetUnmanistedVersion(majorVer, minorVer);
if (majorVer = 6) and (minorVer = 2) then
begin
if osVerInfo.wProductType = VER_NT_WORKSTATION then
Result := 'Windows 8'
else
Result := 'Windows Server 2012'
end;
if (majorVer = 6) and (minorVer = 3) then
begin
if osVerInfo.wProductType = VER_NT_WORKSTATION then
Result := 'Windows 8.1'
else
Result := 'Windows Server 2012R2'
end;
if (majorVer = 10) and (minorVer = 0) then
begin
if osVerInfo.wProductType = VER_NT_WORKSTATION then
Result := 'Windows 10'
else
Result := 'Windows Server 2016'
end;
end
else if (majorVer = 6) and (minorVer = 3) then
begin
if osVerInfo.wProductType = VER_NT_WORKSTATION then
Result := 'Windows 8.1'
else
Result := 'Windows Server 2012R2'
end
else if (majorVer = 6) and (minorVer = 4) then
begin
if osVerInfo.wProductType = VER_NT_WORKSTATION then
Result := 'Windows 10'
else
Result := 'Windows Server 2016'
end
else
Result := 'Unknown';
end;
VER_PLATFORM_WIN32_WINDOWS: { Windows 9x/ME }
begin
if (majorVer = 4) and (minorVer = 0) then
Result := 'Windows 95'
else if (majorVer = 4) and (minorVer = 10) then
begin
if osVerInfo.szCSDVersion[1] = 'A' then
Result := 'Windows 98 SE'
else
Result := 'Windows 98';
end
else if (majorVer = 4) and (minorVer = 90) then
Result := 'Windows ME'
else
Result := 'Unknown';
end;
else
Result := 'Unknown';
end;
end
else
Result := 'Unknown';
end;
function TExeInfo.GetWinDir : String;
//Returns the windows directory
var
pcWindowsDirectory : PChar;
dwWDSize : DWORD;
begin
dwWDSize := MAX_PATH + 1;
GetMem( pcWindowsDirectory, dwWDSize * 2); // allocate memory for the string
try
if Windows.GetWindowsDirectory( pcWindowsDirectory, dwWDSize ) <> 0 then
Result := StrPas(pcWindowsDirectory) + '\';
finally
FreeMem( pcWindowsDirectory ); // now free the memory allocated for the string
end;
end;
function TExeInfo.MajorVersion: integer;
begin
Result := GetVersionPart(0);
end;
function TExeInfo.MinorVersion: integer;
begin
Result := GetVersionPart(1);
end;
function TExeInfo.ReleaseNumber: integer;
begin
Result := GetVersionPart(2);
end;
function TExeInfo.GetSysDir : String;
//Returns system directory
var
pcSystemDirectory : PChar;
dwSDSize : DWORD;
begin
dwSDSize := MAX_PATH + 1;
GetMem( pcSystemDirectory, dwSDSize * 2); // allocate memory for the string
try
if Windows.GetSystemDirectory( pcSystemDirectory, dwSDSize ) <> 0 then
Result := StrPas(pcSystemDirectory) + '\';
finally
FreeMem( pcSystemDirectory ); // now free the memory allocated for the string
end;
end;
function TExeInfo.GetTempDir : String;
//Returns temp directory
var
pcTempDirectory : PChar;
dwSDSize : DWORD;
begin
dwSDSize := MAX_PATH + 1;
GetMem( pcTempDirectory, dwSDSize * 2); // allocate memory for the string
try
if Windows.GetTempPath( dwSDSize, pcTempDirectory ) <> 0 then
Result := pcTempDirectory;
finally
FreeMem( pcTempDirectory ); // now free the memory allocated for the string
end;
end;
function TExeInfo.GetVersion: string;
var
vn: Integer;
begin
vn := GetVersionNr;
Result := IntToStr(Hi(Hiword(vn)))+'.'+IntToStr(Lo(Hiword(vn)))+'.'+IntToStr(Hi(Loword(vn)))+'.'+IntToStr(Lo(Loword(vn)));
end;
function TExeInfo.GetVersionNr: Integer;
begin
Result := MakeLong(MakeWord(BLD_VER,REL_VER),MakeWord(MIN_VER,MAJ_VER));
end;
function TExeInfo.GetVersionPart(idx: integer): integer;
var
sl: TStringList;
verstr: string;
begin
Result := -1;
if FileVersion = '' then
Exit;
verstr := StringReplace(FileVersion,'.',',',[rfReplaceAll]);
sl := TStringList.Create;
try
sl.CommaText := verstr;
if idx < sl.Count then
Result := StrToInt(sl.Strings[idx]);
finally
sl.Free;
end;
end;
procedure TExeInfo.SetVersion(const Value: string);
begin
end;
end.