Debian Codegraph @

project/bundles/vcpkg/installer/process_running.nsh

raw
1 ;; ============================================================
2 ;
3 ; This file is a part of digiKam project
4 ; https://www.digikam.org
5 ;
6 ; Date : 2005-01-01
7 ; Description : Functions to check if executable is running.
8 ; Note: NSIS >= 3 is required to be compatible with Windows 10.
9 ;
10 ; SPDX-FileCopyrightText: 2005-2017 by Tim Kosse <tim dot kosse at filezilla-project dot org>
11 ; SPDX-FileCopyrightText: 2017-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
12 ;
13 ; SPDX-License-Identifier: GPL-2.0-or-later
14 ;
15 ; ============================================================ ;;
16
17 !ifndef EXECUTABLE_RUNNING_INCLUDED
18 !define EXECUTABLE_RUNNING_INCLUDED
19
20 !include "LogicLib.nsh"
21
22 ; Returns number of processes * 4 on top of stack,
23 ; array with all processes right below it.
24 ; Array should be cleared using System::Free
25
26 !macro ENUMPROCESSES un
27
28 Function ${un}EnumProcesses
29
30 ; Is this really necessary for $Rx?
31 Push $R1
32 Push $R0
33 Push $R2
34 Push $R3
35
36 ; Double size of array each time EnumProcesses fills it completely so that
37 ; we do get all processes
38
39 StrCpy $R1 1024
40
41 enum_processes_loop:
42
43 System::Alloc $R1
44
45 Pop $R0
46
47 System::Call "psapi::EnumProcesses(i R0, i R1, *i .R2) i .R3"
48
49 ${If} $R3 == 0
50 ; EnumProcesses failed, how can that be? :P
51 goto enum_processes_fail
52 ${EndIf}
53
54 ${If} $R1 == $R2
55
56 ; Too small buffer. Retry with twice the size
57
58 Intop $R1 $R1 * 2
59 System::Free $R0
60
61 goto enum_processes_loop
62
63 ${EndIf}
64
65 StrCpy $R1 $R2
66
67 ; Restore registers
68 ; and put results on stack
69 Pop $R3
70 Pop $R2
71 Exch $R0
72 Exch
73 Exch $R1
74 return
75
76 enum_processes_fail:
77
78 Pop $R3
79 Pop $R2
80 Pop $R0
81 Pop $R1
82
83 Push 0
84 Push 0
85
86 FunctionEnd
87
88 !macroend
89
90 ; Insert function as an installer and uninstaller function.
91 !insertmacro ENUMPROCESSES ""
92 !insertmacro ENUMPROCESSES "un."
93
94 ;-------------------------------------------
95
96 ; Expects process ID on top of stack, returns
97 ; filename (in device syntax) on top of stack
98
99 !macro GETFILENAMEFROMPROCESSID un
100
101 Function ${un}GetFilenameFromProcessId
102
103 Exch $R0
104 Push $R1
105 Push $R2
106 Push $R3
107
108 !define ${un}PROCESS_QUERY_INFORMATION 0x0400
109 System::Call "kernel32::OpenProcess(i ${${un}PROCESS_QUERY_INFORMATION}, i 0, i $R0) i .R0"
110
111 ${If} $R0 == 0
112
113 Pop $R3
114 Pop $R2
115 Pop $R1
116 Pop $R0
117 Push ''
118 return
119
120 ${EndIf}
121
122 StrCpy $R3 ${NSIS_MAX_STRLEN}
123 System::Call "kernel32::QueryFullProcessImageName(i R0, i 0, t .R1, *i R3) i .R2"
124
125 ${If} $R2 == 0
126 ; Fallback
127 System::Call "psapi::GetProcessImageFileName(i R0, t .R1, i ${NSIS_MAX_STRLEN}) i .R2"
128 ${EndIf}
129
130 ${If} $R2 == 0
131
132 System::Call "kernel32::CloseHandle(i R0)"
133 Pop $R3
134 Pop $R2
135 Pop $R1
136 Pop $R0
137 Push ''
138
139 return
140
141 ${EndIf}
142
143 System::Call "kernel32::CloseHandle(i R0)"
144
145 Pop $R3
146 Pop $R2
147 StrCpy $R0 $R1
148 Pop $R1
149 Exch $R0
150
151 FunctionEnd
152
153 !macroend
154
155 ; Insert function as an installer and uninstaller function.
156 !insertmacro GETFILENAMEFROMPROCESSID ""
157 !insertmacro GETFILENAMEFROMPROCESSID "un."
158
159 ;-------------------------------------------
160
161 ; Expects process name on top of stack. Afterwards, top of stack contains path
162 ; to the process if it's running or an empty string if it is not.
163
164 !macro ISPROCESSRUNNING un
165
166 Function ${un}IsProcessRunning
167
168 Exch $R0 ; Name
169
170 Push $R1 ; Bytes
171 Push $R2 ; Array
172 Push $R3 ; Counter
173 Push $R4 ; Strlen
174 Push $R5 ; Current process ID and image filename
175 Push $R6 ; Last part of path
176
177 StrCpy $R0 "\$R0"
178
179 StrLen $R4 $R0
180 IntOp $R4 0 - $R4
181
182 Call ${un}EnumProcesses
183
184 Pop $R1
185 Pop $R2
186
187 StrCpy $R3 0
188
189 ${While} $R3 < $R1
190
191 IntOp $R5 $R2 + $R3
192
193 System::Call "*$R5(i .R5)"
194 Push $R5
195
196 Call ${un}GetFilenameFromProcessId
197
198 Pop $R5
199
200 ; Get last part of filename
201 StrCpy $R6 $R5 '' $R4
202
203 ${If} $R6 == $R0
204
205 ; Program is running
206 StrCpy $R0 $R5
207 Pop $R6
208 Pop $R5
209 Pop $R4
210 Pop $R3
211 Pop $R2
212 Pop $R1
213 Exch $R0
214 return
215
216 ${EndIf}
217
218 IntOp $R3 $R3 + 4
219
220 ${EndWhile}
221
222 Pop $R5
223 Pop $R4
224 Pop $R3
225 Pop $R2
226 Pop $R1
227 Pop $R0
228 Push ''
229
230 FunctionEnd
231
232 !macroend
233
234 ; Insert function as an installer and uninstaller function.
235 !insertmacro ISPROCESSRUNNING ""
236 !insertmacro ISPROCESSRUNNING "un."
237
238 ;-------------------------------------------
239
240 !macro CHECKDIGIKAM un
241
242 Function ${un}CheckDigikamRunning
243
244 Push "digikam.exe"
245 Call ${un}IsProcessRunning
246 Pop $R1
247
248 ${While} $R1 != ''
249
250 MessageBox MB_ABORTRETRYIGNORE|MB_DEFBUTTON2 "digiKam appears to be running.$\nPlease close all running instances of digiKam before continuing the installation." /SD IDIGNORE IDABORT CheckDigikamRunning_abort IDIGNORE CheckDigikamRunning_ignore
251
252 Push "digikam.exe"
253 Call ${un}IsProcessRunning
254 Pop $R1
255
256 ${EndWhile}
257
258 CheckDigikamRunning_ignore:
259 Return
260
261 CheckDigikamRunning_abort:
262 Quit
263
264 FunctionEnd
265
266 !macroend
267
268 ; Insert function as an installer and uninstaller function.
269 !insertmacro CHECKDIGIKAM ""
270 !insertmacro CHECKDIGIKAM "un."
271
272 ;-------------------------------------------
273
274 !macro CHECKSHOWFOTO un
275
276 Function ${un}CheckShowfotoRunning
277
278 Push "showfoto.exe"
279 Call ${un}IsProcessRunning
280 Pop $R1
281
282 ${While} $R1 != ''
283
284 MessageBox MB_ABORTRETRYIGNORE|MB_DEFBUTTON2 "Showfoto appears to be running.$\nPlease close all running instances of Showfoto before continuing the installation." /SD IDIGNORE IDABORT CheckShowfotoRunning_abort IDIGNORE CheckShowfotoRunning_ignore
285
286 Push "showfoto.exe"
287 Call ${un}IsProcessRunning
288 Pop $R1
289
290 ${EndWhile}
291
292 CheckShowfotoRunning_ignore:
293 Return
294
295 CheckShowfotoRunning_abort:
296 Quit
297
298 FunctionEnd
299
300 !macroend
301
302 ; Insert function as an installer and uninstaller function.
303 !insertmacro CHECKSHOWFOTO ""
304 !insertmacro CHECKSHOWFOTO "un."
305
306 ;-------------------------------------------
307
308 !endif ;EXECUTABLE_RUNNING_INCLUDED