-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsUtil.pp
99 lines (82 loc) · 2.57 KB
/
wsUtil.pp
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
{ Copyright (C) 2023 by Bill Stewart (bstewart at iname.com)
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Lesser Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see https://www.gnu.org/licenses/.
}
unit wsUtil;
{$MODE OBJFPC}
{$MODESWITCH UNICODESTRINGS}
interface
uses
Windows;
function CountBits(N: Integer): Integer;
function GetFileVersion(const FileName: string): string;
function StrToInt(const S: string; var I: Integer): Boolean;
implementation
function CountBits(N: Integer): Integer;
var
Count: Integer;
begin
Count := 0;
while N <> 0 do
begin
Count := Count + (N and 1);
N := N shr 1;
end;
result := Count;
end;
function StrToInt(const S: string; var I: Integer): Boolean;
var
Code: Word;
begin
Val(S, I, Code);
result := Code = 0;
end;
function IntToStr(const I: LongInt): string;
begin
Str(I, result);
end;
function GetFileVersion(const FileName: string): string;
var
VerInfoSize, Handle: DWORD;
pBuffer: Pointer;
pFileInfo: ^VS_FIXEDFILEINFO;
Len: UINT;
begin
result := '';
VerInfoSize := GetFileVersionInfoSizeW(PChar(FileName), // LPCWSTR lptstrFilename
Handle); // LPDWORD lpdwHandle
if VerInfoSize > 0 then
begin
GetMem(pBuffer, VerInfoSize);
if GetFileVersionInfoW(PChar(FileName), // LPCWSTR lptstrFilename
Handle, // DWORD dwHandle
VerInfoSize, // DWORD dwLen
pBuffer) then // LPVOID lpData
begin
if VerQueryValueW(pBuffer, // LPCVOID pBlock
'\', // LPCWSTR lpSubBlock
pFileInfo, // LPVOID *lplpBuffer
Len) then // PUINT puLen
begin
with pFileInfo^ do
begin
result := IntToStr(HiWord(dwFileVersionMS)) + '.' +
IntToStr(LoWord(dwFileVersionMS)) + '.' +
IntToStr(HiWord(dwFileVersionLS));
// LoWord(dwFileVersionLS) intentionally omitted
end;
end;
end;
FreeMem(pBuffer);
end;
end;
begin
end.