-
Notifications
You must be signed in to change notification settings - Fork 15
/
smartmontools for Windows includes.iss
282 lines (249 loc) · 7.58 KB
/
smartmontools for Windows includes.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
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
//// General purpose functions (2020111701)
[code]
//// Returns true if IsWin64 is false
function IsWin32(): Boolean;
begin
result := not IsWin64;
end;
//// Returns last char of a string
function LastChar(str: String): String;
begin
result := copy(str, Length(str), 1);
end;
//// Checks if given service exists
function ServiceExists(srv: String): Boolean;
var resultcode: Integer;
begin
ShellExec('', ExpandConstant('{sys}\sc.exe'), 'query ' + srv, '', SW_HIDE, ewWaitUntilTerminated, resultcode);
if resultcode = 0 then
Result := true
else
Result := false
end;
//// Checks if given service runs
function ServiceIsRunning(srv: String): Boolean;
var resultcode: Integer;
begin
ShellExec('', ExpandConstant('{cmd}'), ExpandConstant(' /C {sys}\sc.exe query ' + srv + ' | findstr "RUNNING"'), '', SW_HIDE, ewWaitUntilTerminated, resultcode);
if resultcode = 0 then
Result := true
else
Result := false
end;
//// Starts service after installation
function LoadService(srv: String): Integer;
var resultcode: Integer;
begin
if (ServiceExists(srv)) then
begin
if (ServiceIsRunning(srv) = false) then
begin
ShellExec('', ExpandConstant('{sys}\sc.exe'), 'start '+ srv, '', SW_HIDE, ewWaitUntilTerminated, resultcode);
if resultcode <> 0 then
MsgBox('Cannot load service [' + srv + '], exit code = ' + IntToStr(resultcode), mbError, MB_OK);
result := resultcode;
end
end
else
MsgBox('Service [' + srv + '] cannot be loaded because it does not exist.', mbError, MB_OK);
end;
//// Stops service and wait 2000ms before removing file to be sure it's not in use anymore
function UnloadService(srv: String): Integer;
var resultcode: Integer;
begin
if (ServiceIsRunning(srv) = true) then
begin
ShellExec('', ExpandConstant('{sys}\sc.exe'), 'stop ' + srv, '', SW_HIDE, ewWaitUntilTerminated, resultcode);
sleep(2000);
if resultcode <> 0 then
MsgBox('Cannot unload service [' + srv + '], exit code = ' + IntToStr(resultcode), mbError, MB_OK);
result := resultcode;
end
else
result := 0;
end;
//// Checks if binary path of service belongs to smartmontools-win
function IsSmartWinService(srv: String): Boolean;
var resultcode: Integer;
begin
ShellExec('', ExpandConstant('{cmd}'), ExpandConstant('/C {sys}\sc.exe qc ' + srv + ' | findstr /C:"\\smartmontools for Windows\\bin\\smartd.exe"'), '', SW_HIDE, ewWaitUntilTerminated, resultcode);
if resultcode = 0 then
result := true
else
result := false
end;
//// Installs smartd service
procedure InstallService();
var resultCode: Integer;
begin
Exec(ExpandConstant('{app}\bin\smartd.exe'), ExpandConstant('install -c "{app}\bin\smartd.conf"'), '', SW_HIDE, ewWaitUntilTerminated, resultcode);
if resultcode <> 0 then
MsgBox('Cannot install service via [' + ExpandConstant('{app}\bin\smartd.exe') + ' ' + ExpandConstant('install -c "{app}\bin\smartd.conf"') + '], exit code = ' + IntToStr(resultcode), mbError, MB_OK);
end;
//// Uninstalls given service
procedure UninstallService(srv: String);
var resultcode: Integer;
begin
UnloadService(srv);
ShellExec('', ExpandConstant('{sys}\sc.exe'), 'delete ' + srv, '', SW_HIDE, ewWaitUntilTerminated, resultcode);
if resultcode <> 0 then
MsgBox('Cannot uninstall service [' + srv + '], exit code = ' + IntToStr(resultcode), mbError, MB_OK);
end;
//// Explode a string into an array using passed delimeter
function Explode(Text: String; Separator: String): TArrayOfString;
var
i: integer;
res: TArrayOfString;
begin
i := 0;
repeat
SetArrayLength(res, i+1);
if Pos(Separator,Text) > 0 then begin
res[i] := Copy(Text, 1, Pos(Separator, Text)-1);
Text := Copy(Text, Pos(Separator,Text) + Length(Separator), Length(Text));
i := i + 1;
end else
begin
res[i] := Text;
Text := '';
end;
until Length(Text)=0;
result := res;
end;
//// Implode an array into a string using a passed delimiter
function Implode(Arr: TArrayOfString; Separator: String): String;
var
i: integer;
res: String;
begin
i := 0;
res := '';
repeat
res := res + Arr[i] + Separator;
i := i + 1;
until (i >= GetArrayLength(Arr));
result := res;
end;
//// Returns commmand line arguments, usage: GetCommandLineParam('--test')
////("--test=value" will return "value", "-t value" will return "value", "-t" will return "yes"
function GetCommandLineParam(Param: string): String;
var
CmdlineParamCount: Integer;
begin
CmdlineParamCount := 0;
while (CmdlineParamCount <= ParamCount) do
begin
if ((Param = Copy(ParamStr(CmdlineParamCount), 0, (Pos('=', ParamStr(CmdlineParamCount))) - 1)) or (Param = ParamStr(CmdlineParamCount))) then
begin
if (Pos('=',ParamStr(CmdlineParamCount)) > 0) then
begin
result := Copy(ParamStr(CmdlineParamCount), (Pos('=', ParamStr(CmdlineParamCount)) + 1), Length(ParamStr(CmdlineParamCount)));
end
else if ((Copy(ParamStr(CmdlineParamCount + 1), 0, 1) <> '-') and (Copy(ParamStr(CmdlineParamCount + 1), 0, 1) <> '/') and (Length(ParamStr(CmdlineParamCount + 1)) > 0)) then
//end else if ((Pos('-', ParamStr(CmdlineParamCount + 1)) <> 0) and (Length(ParamStr(CmdlineParamCount + 1)) > 0) and (Pos('/', ParamStr(CmdlineParamCount + 1)) <> 0)) then
begin
result := ParamStr(CmdlineParamCount + 1);
end else
begin
result := 'yes';
end;
end;
// and
//MsgBox(ParamStr(CmdlineParamCount), MbInformation, MB_OK);
CmdlineParamCount := CmdlineParamCount + 1;
end;
end;
// Determine if an earlier version of this program is already installed
function IsUpdateInstall(): Boolean;
begin
Result := RegKeyExists(HKEY_LOCAL_MACHINE, 'Software\Microsoft\Windows\CurrentVersion\Uninstall\{#AppGUID}_is1') or RegKeyExists(HKEY_LOCAL_MACHINE, 'Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{#AppGUID}_is1');
end;
// Base64 encode and decode functions found at http://www.vincenzo.net/isxkb/index.php?title=Encode/Decode_Base64
const Codes64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
function Encode64(S: AnsiString): AnsiString;
var
i: Integer;
a: Integer;
x: Integer;
b: Integer;
begin
Result := '';
a := 0;
b := 0;
for i := 1 to Length(s) do
begin
x := Ord(s[i]);
b := b * 256 + x;
a := a + 8;
while (a >= 6) do
begin
a := a - 6;
x := b div (1 shl a);
b := b mod (1 shl a);
Result := Result + copy(Codes64,x + 1,1);
end;
end;
if a > 0 then
begin
x := b shl (6 - a);
Result := Result + copy(Codes64,x + 1,1);
end;
a := Length(Result) mod 4;
if a = 2 then
Result := Result + '=='
else if a = 3 then
Result := Result + '=';
end;
function Decode64(S: AnsiString): AnsiString;
var
i: Integer;
a: Integer;
x: Integer;
b: Integer;
begin
Result := '';
a := 0;
b := 0;
for i := 1 to Length(s) do
begin
x := Pos(s[i], codes64) - 1;
if x >= 0 then
begin
b := b * 64 + x;
a := a + 6;
if a >= 8 then
begin
a := a - 8;
x := b shr a;
b := b mod (1 shl a);
x := x mod 256;
Result := Result + chr(x);
end;
end
else
Exit; // finish at unknown
end;
end;
function FileReplaceString(const FileName, SearchString, ReplaceString: string):boolean;
var
MyFile : TStrings;
MyText : string;
begin
MyFile := TStringList.Create;
try
result := true;
try
MyFile.LoadFromFile(FileName);
MyText := MyFile.Text;
if StringChangeEx(MyText, SearchString, ReplaceString, True) > 0 then //Only save if text has been changed.
begin;
MyFile.Text := MyText;
MyFile.SaveToFile(FileName);
end;
except
result := false;
end;
finally
MyFile.Free;
end;
end;