source: filezilla/trunk/fuentes/data/process_running.nsh @ 130

Last change on this file since 130 was 130, checked in by jrpelegrina, 3 years ago

First release to xenial

File size: 2.7 KB
Line 
1!ifndef EXECUTABLE_RUNNING_INCLUDED
2!define EXECUTABLE_RUNNING_INCLUDED
3
4!include "LogicLib.nsh"
5
6; Returns number of processes * 4 on top of stack,
7; array with all processes right below it.
8; Array should be cleared using System::Free
9
10Function EnumProcesses
11
12  ; Is this really necesssary for $Rx?
13  Push $R1
14  Push $R0
15  Push $R2
16  Push $R3
17
18  ; Double size of array each time EnumProcesses fills it completely so that
19  ; we do get all processes
20
21  StrCpy $R1 1024
22
23 enum_processes_loop:
24
25  System::Alloc $R1
26
27  Pop $R0
28
29  System::Call "psapi::EnumProcesses(i R0, i R1, *i .R2) i .R3"
30
31  ${If} $R3 == 0
32    ; EnumProcesses failed, how can that be? :P
33    goto enum_processes_fail
34  ${EndIf}
35
36  ${If} $R1 == $R2
37
38    ; Too small buffer. Retry with twice the size
39
40    Intop $R1 $R1 * 2
41    System::Free $R0
42
43    goto enum_processes_loop
44
45  ${EndIf}
46
47  StrCpy $R1 $R2
48
49  ; Restore registers
50  ; and put results on stack
51  Pop $R3
52  Pop $R2
53  Exch $R0
54  Exch
55  Exch $R1
56  return
57
58 enum_processes_fail:
59
60  Pop $R3
61  Pop $R2
62  Pop $R0
63  Pop $R1
64
65  Push 0
66  Push 0
67
68FunctionEnd
69
70; Expects process ID on top of stack, returns
71; filename (in device syntax) on top of stack
72Function GetFilenameFromProcessId
73
74  Exch $R0
75  Push $R1
76  Push $R2
77
78  !define PROCESS_QUERY_INFORMATION 0x0400
79  System::Call "kernel32::OpenProcess(i ${PROCESS_QUERY_INFORMATION}, i 0, i $R0) i .R0"
80
81  ${If} $R0 == 0
82
83    Pop $R2
84    Pop $R1
85    Pop $R0
86    Push ''
87    return
88
89  ${EndIf}
90
91  System::Call "psapi::GetProcessImageFileName(i R0, t .R1, i ${NSIS_MAX_STRLEN}) i .R2"
92
93  ${If} $R2 == 0
94
95    System::Call "kernel32::CloseHandle(i R0)"
96    Pop $R2
97    Pop $R1
98    Pop $R0
99    Push ''
100
101    return
102
103  ${EndIf}
104
105  System::Call "kernel32::CloseHandle(i R0)"
106
107  Pop $R2
108  StrCpy $R0 $R1
109  Pop $R1
110  Exch $R0
111
112FunctionEnd
113
114; Expects process name on top of stack
115; Gets replaced with 1 if process is running,
116; 0 if it is not
117Function IsProcessRunning
118
119  Exch $R0 ; Name
120
121  Push $R1 ; Bytes
122  Push $R2 ; Array
123  Push $R3 ; Counter
124  Push $R4 ; Strlen
125  Push $R5 ; Current process ID and image filename
126
127  StrCpy $R0 "\$R0"
128
129  StrLen $R4 $R0
130  IntOp $R4 0 - $R4
131
132  Call EnumProcesses
133
134  Pop $R1
135  Pop $R2
136
137  StrCpy $R3 0
138
139  ${While} $R3 < $R1
140
141    IntOp $R5 $R2 + $R3
142
143    System::Call "*$R5(i .R5)"
144    Push $R5
145
146    Call GetFilenameFromProcessId
147
148    Pop $R5
149
150    ; Get last part of filename
151    StrCpy $R5 $R5 '' $R4
152
153    ${If} $R5 == $R0
154
155      ; Program is running
156      Pop $R5
157      Pop $R4
158      Pop $R3
159      Pop $R2
160      Pop $R1
161      Pop $R0
162      Push 1
163      return
164
165    ${EndIf}
166
167    IntOp $R3 $R3 + 4
168
169  ${EndWhile}
170
171  Pop $R5
172  Pop $R4
173  Pop $R3
174  Pop $R2
175  Pop $R1
176  Pop $R0
177  Push 0
178
179FunctionEnd
180
181!endif ;EXECUTABLE_RUNNING_INCLUDED
Note: See TracBrowser for help on using the repository browser.