/
util.iss
209 lines (183 loc) · 6.14 KB
/
util.iss
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
// Inno Setup utilities
//
// Copyright (c) 2009-2012 Jon Maken
// Revision: 2012-05-28 14:06:56 -0600
// License: Modified BSD License
// forward declarations
function MungePathish(const SrcList: TStringList; NewData: Array of String; RegValue: String; IsUninstalling: Boolean): Boolean; forward;
procedure ModifyPathish(NewData: Array of String; RegValue, Delim: String); forward;
function StrToList(const SrcString: String; Delim: String): TStringList; forward;
function ListToStr(const SrcList: TStringList; Delim: String): String; forward;
function IsAdmin(): Boolean;
begin
Result := IsAdminLoggedOn or IsPowerUserLoggedOn;
end;
function IsNotAdmin(): Boolean;
begin
Result := not (IsAdminLoggedOn or IsPowerUserLoggedOn);
end;
function GetUserHive(): Integer;
begin
if IsAdminLoggedOn or IsPowerUserLoggedOn then
Result := HKLM
else
Result := HKCU;
end;
function GetEnvironmentKey(): String;
begin
if IsAdmin then
Result := 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
else
Result := 'Environment';
end;
procedure ModifyFileExts(Exts: Array of String);
begin
ModifyPathish(Exts, 'PATHEXT', ';');
end;
procedure ModifyPath(Exts: Array of String);
begin
ModifyPathish(Exts, 'PATH', ';');
end;
// Modifies path-like registry keys such as PATH, PATHEXT
procedure ModifyPathish(NewData: Array of String; RegValue, Delim: String);
var
NeedRegChange: Boolean;
RootKey: Integer;
SubKey, OrigData, NewPathish, Tmp, TmpExpandable: String;
PathishList: TStringList;
begin
RootKey := GetUserHive;
SubKey := GetEnvironmentKey;
try
RegQueryStringValue(RootKey, SubKey, RegValue, OrigData);
Log('Original ' + AnsiUppercase(RegValue) + ': ' + OrigData);
// ensure originally empty users PATHEXT also contains system values
if (RootKey = HKCU) and (AnsiUppercase(RegValue) = 'PATHEXT') and (OrigData = '') then
begin
Log('Empty HKCU ' + AnsiUppercase(RegValue) + ', prepending %PATHEXT% to new value');
OrigData := ('%' + RegValue + '%');
end;
PathishList := StrToList(OrigData, Delim);
NeedRegChange := MungePathish(PathishList, NewData, RegValue, IsUninstaller);
if NeedRegChange then
begin
NewPathish := ListToStr(PathishList, ';');
case AnsiUppercase(RegValue) of
'PATH': RegWriteExpandStringValue(RootKey, SubKey, 'Path', NewPathish);
'PATHEXT': RegWriteExpandStringValue(RootKey, SubKey, 'PATHEXT', NewPathish);
end;
Log(AnsiUppercase(RegValue) + ' updated to: ' + NewPathish);
// remove values if empty after uninstaller reverts its mods
if IsUninstaller then
begin
if RegQueryStringValue(RootKey, SubKey, RegValue, Tmp) then
begin
// If the key is empty or expandable version (%RegValue%), remove it.
TmpExpandable := '%' + RegValue + '%';
if (Tmp = '') or (Tmp = TmpExpandable) then
begin
RegDeleteValue(RootKey, SubKey, RegValue);
Log('uninstaller deleted empty ' + AnsiUppercase(RegValue) +
' to match original config');
end;
end;
end;
end else // no reg change needed
Log('no changes need for ' + AnsiUppercase(RegValue));
finally
PathishList.Free;
end;
end;
function StrToList(const SrcString: String; Delim: String): TStringList;
var
PathList: TStringList;
TmpPath: String;
begin
PathList := TStringList.Create;
// empty PATH
if Length(SrcString) = 0 then Result := PathList;
if (Length(SrcString) > 0) then
begin
// single entry with no trailing ';'
if Pos(Delim, SrcString) = 0 then
begin
PathList.Append(SrcString);
Result := PathList;
// single entry with trailing ';'
// TODO address pathological case of multiple trailing ';' chars?
end else if Pos(Delim, SrcString) = Length(SrcString) then
begin
TmpPath := SrcString;
StringChangeEx(TmpPath, Delim, '', True);
PathList.Append(TmpPath);
Result := PathList;
end else
// multiple entries
begin
TmpPath := SrcString;
// clean up a leading ';' pathological case if it exists
if Pos(Delim, TmpPath) = 1 then TmpPath := Copy(TmpPath, 2, Length(TmpPath));
while (Pos(Delim, TmpPath) > 0) do
begin
PathList.Append(Copy(TmpPath, 1, Pos(Delim, TmpPath) - 1));
TmpPath := Copy(TmpPath, Pos(Delim, TmpPath) + 1, Length(TmpPath));
end;
// add final remaining dir if not empty due to trailing ';'
if Length(TmpPath) > 0 then PathList.Append(TmpPath);
Result := PathList;
end;
end;
end;
function ListToStr(const SrcList: TStringList; Delim: String): String;
var
Path: String;
I: Integer;
begin
for I := 0 to SrcList.Count - 1 do begin
if Length(Path) = 0 then
begin
Path := SrcList[I];
Continue;
end;
Path := Path + Delim + SrcList[I];
end;
Result := Path;
end;
function MungePathish(const SrcList: TStringList; NewData: Array of String;
RegValue: String; IsUninstalling: Boolean): Boolean;
var
RootKey, I, N: Integer;
Item: String;
RegChangeFlag: Boolean;
begin
RegChangeFlag := False;
RootKey := GetUserHive;
for N := 0 to GetArrayLength(NewData) - 1 do
begin
Item := NewData[N];
if not IsUninstalling then // installing...
begin
// update PathishList ONLY if new item isn't already in the list
// DO NOT ASSUME that the installer should duplicate entry or change order!
if SrcList.IndexOf(Item) = -1 then
begin
case AnsiUppercase(RegValue) of
'PATH': SrcList.Insert(0, Item);
'PATHEXT': SrcList.Add(AnsiUppercase(Item));
end;
RegChangeFlag := True;
end else // already in existing config, no need for update; log it
Log(Item + ' already on ' + AnsiUppercase(RegValue) +
' in original config; not modifying ' + AnsiUppercase(RegValue));
end else // uninstalling...
begin
I := SrcList.IndexOf(Item);
if I <> -1 then // found on PathishList, delete it
begin
SrcList.Delete(I);
RegChangeFlag := True;
end; // not found on PathishList, no need for registry mod
end;
end;
Result := RegChangeFlag;
end;