-
Notifications
You must be signed in to change notification settings - Fork 2
/
wsISPackage.pp
316 lines (283 loc) · 7.18 KB
/
wsISPackage.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
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
{ Copyright (C) 2021-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/.
}
{$MODE OBJFPC}
{$MODESWITCH UNICODESTRINGS}
unit wsISPackage;
interface
uses
Windows;
const
ERROR_UNKNOWN_PRODUCT = 1605;
ERROR_BAD_CONFIGURATION = 1610;
type
TInnoSetupPackage = class
private
MyIs64Bit, MyIsAdmin: Boolean;
MyAppId, MySubKeyName: string;
MyRootKey: HKEY;
public
constructor Create();
procedure Init(const AppId: string; const Is64BitInstallMode, IsAdminInstallMode: Boolean);
function IsInstalled(): DWORD;
function GetVersion(): string;
function CompareVersion(const InstallingVersion: string): LongInt;
function Uninstall(): DWORD;
destructor Destroy(); override;
end;
var
InnoSetupPackage: TInnoSetupPackage;
// Returns true if the string is a valid version string, or false otherwise
function wsTestVersionString(const S: string): Boolean;
// Compares two version strings 'a[.b[.c[.d]]]'
// Returns:
// < 0 if V1 < V2
// 0 if V1 = V2
// > 0 if V1 > V2
function wsCompareVersionStrings(const V1, V2: string): LongInt;
implementation
uses
wsUtilFile,
wsUtilReg;
type
TStringArray = array of string;
TVersionArray = array[0..3] of Word;
// Returns the number of times Substring appears in S
function CountSubstring(const Substring, S: string): LongInt;
var
P: LongInt;
begin
result := 0;
P := Pos(Substring, S, 1);
while P <> 0 do
begin
Inc(result);
P := Pos(Substring, S, P + Length(Substring));
end;
end;
// Splits S into the Dest array using Delim as a delimiter
procedure StrSplit(S, Delim: string; var Dest: TStringArray);
var
I, P: LongInt;
begin
I := CountSubstring(Delim, S);
// If no delimiters, Dest is a single-element array
if I = 0 then
begin
SetLength(Dest, 1);
Dest[0] := S;
exit;
end;
SetLength(Dest, I + 1);
for I := 0 to Length(Dest) - 1 do
begin
P := Pos(Delim, S);
if P > 0 then
begin
Dest[I] := Copy(S, 1, P - 1);
Delete(S, 1, P + Length(Delim) - 1);
end
else
Dest[I] := S;
end;
end;
function StrToInt(const S: string; var I: LongInt): Boolean;
var
Code: Word;
begin
Val(S, I, Code);
result := Code = 0;
end;
function StrToWord(const S: string; var W: Word): Boolean;
var
Code: Word;
begin
Val(S, W, Code);
result := Code = 0;
end;
function GetVersionArray(const S: string; var Version: TVersionArray): Boolean;
var
A: TStringArray;
ALen, I, Part: LongInt;
begin
result := false;
StrSplit(S, '.', A);
ALen := Length(A);
if ALen > 4 then
exit;
if ALen < 4 then
begin
SetLength(A, 4);
for I := ALen to 3 do
A[I] := '0';
end;
for I := 0 to Length(A) - 1 do
begin
result := StrToInt(A[I], Part);
if not result then
exit;
result := (Part >= 0) and (Part <= $FFFF);
if not result then
exit;
end;
for I := 0 to 3 do
begin
result := StrToWord(A[I], Version[I]);
if not result then
exit;
end;
end;
function wsTestVersionString(const S: string): Boolean;
var
Version: TVersionArray;
begin
result := GetVersionArray(S, Version);
end;
function wsCompareVersionStrings(const V1, V2: string): LongInt;
var
Ver1, Ver2: TVersionArray;
I: LongInt;
Word1, Word2: Word;
begin
result := 0;
if not GetVersionArray(V1, Ver1) then
exit;
if not GetVersionArray(V2, Ver2) then
exit;
for I := 0 to 3 do
begin
Word1 := Ver1[I];
Word2 := Ver2[I];
if Word1 > Word2 then
begin
result := 1;
exit;
end
else if Word1 < Word2 then
begin
result := -1;
exit;
end;
end;
end;
constructor TInnoSetupPackage.Create();
begin
MyIs64Bit := false;
MyIsAdmin := false;
MyAppId := '';
MySubKeyName := '';
MyRootKey := 0;
end;
procedure TInnoSetupPackage.Init(const AppId: string; const Is64BitInstallMode, IsAdminInstallMode: Boolean);
begin
if AppId = '' then
exit();
MyAppId := AppId;
MyIs64Bit := Is64BitInstallMode;
MyIsAdmin := IsAdminInstallMode;
if MyIs64Bit then
begin
if MyIsAdmin then
MyRootKey := HKEY_LOCAL_MACHINE_64
else
MyRootKey := HKEY_CURRENT_USER_64;
end
else
begin
if MyIsAdmin then
MyRootKey := HKEY_LOCAL_MACHINE_32
else
MyRootKey := HKEY_CURRENT_USER_32;
end;
MySubKeyName := 'Software\Microsoft\Windows\CurrentVersion\Uninstall\' + MyAppId + '_is1';
end;
function TInnoSetupPackage.IsInstalled(): DWORD;
begin
// Must call Init() first
if MyAppId = '' then
exit(ERROR_INVALID_PARAMETER)
else
begin
if RegKeyExists(MyRootKey, MySubKeyName) then
result := 1
else
result := 0;
end;
end;
function TInnoSetupPackage.GetVersion(): string;
var
DisplayVersion: string;
begin
result := '';
if myAppId <> '' then
begin
if RegQueryStringValue(MyRootKey, MySubKeyName, 'DisplayVersion', DisplayVersion) and (DisplayVersion <> '') then
result := DisplayVersion;
end;
end;
function TInnoSetupPackage.CompareVersion(const InstallingVersion: string): LongInt;
var
CurrentVersion: string;
begin
result := 0;
CurrentVersion := GetVersion();
if (CurrentVersion <> '') and (InstallingVersion <> '') then
result := wsCompareVersionStrings(InstallingVersion, CurrentVersion);
end;
function TInnoSetupPackage.Uninstall(): DWORD;
var
UninstallString, UninstallerFileName: string;
P, ProcessExitCode: DWORD;
begin
// Must call Init() first
if MyAppId = '' then
exit(ERROR_INVALID_PARAMETER);
// Package not detected
if IsInstalled() <> 1 then
exit(ERROR_UNKNOWN_PRODUCT);
if (not RegQueryStringValue(MyRootKey, MySubKeyName, 'UninstallString', UninstallString)) or (UninstallString = '') then
exit(ERROR_BAD_CONFIGURATION);
// Get uninstaller file name
UninstallerFileName := UninstallString;
// Remove '"' characters
P := Pos('"', UninstallerFileName);
while P > 0 do
begin
Delete(UninstallerFileName, P, 1);
P := Pos('"', UninstallerFileName);
end;
if not FileExists(UninstallerFileName) then
exit(ERROR_BAD_CONFIGURATION);
// Run uninstaller executable and wait until it closes
UninstallString := '"' + UninstallerFileName + '" /SILENT /SUPPRESSMSGBOXES /NORESTART';
result := StartProcess(UninstallString, ProcessExitCode);
if result = 0 then
begin
if ProcessExitCode = 0 then
// Wait for uninstaller executable to be deleted
while FileExists(UninstallerFileName) do
Sleep(100);
result := ProcessExitCode;
end;
end;
destructor TInnoSetupPackage.Destroy();
begin
end;
initialization
begin
InnoSetupPackage := TInnoSetupPackage.Create();
end;
finalization
begin
InnoSetupPackage.Destroy();
end;
end.