File: process_running.nsh

package info (click to toggle)
filezilla 3.69.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 31,772 kB
  • sloc: cpp: 97,617; ansic: 54,984; sh: 5,225; makefile: 2,080; xml: 375
file content (192 lines) | stat: -rw-r--r-- 3,029 bytes parent folder | download | duplicates (6)
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
!ifndef EXECUTABLE_RUNNING_INCLUDED
!define EXECUTABLE_RUNNING_INCLUDED

!include "LogicLib.nsh"

; Returns number of processes * 4 on top of stack,
; array with all processes right below it.
; Array should be cleared using System::Free

Function EnumProcesses

  ; Is this really necesssary for $Rx?
  Push $R1
  Push $R0
  Push $R2
  Push $R3

  ; Double size of array each time EnumProcesses fills it completely so that
  ; we do get all processes

  StrCpy $R1 1024

 enum_processes_loop:

  System::Alloc $R1

  Pop $R0

  System::Call "psapi::EnumProcesses(i R0, i R1, *i .R2) i .R3"

  ${If} $R3 == 0
    ; EnumProcesses failed, how can that be? :P
    goto enum_processes_fail
  ${EndIf}

  ${If} $R1 == $R2

    ; Too small buffer. Retry with twice the size

    Intop $R1 $R1 * 2
    System::Free $R0

    goto enum_processes_loop

  ${EndIf}

  StrCpy $R1 $R2

  ; Restore registers
  ; and put results on stack
  Pop $R3
  Pop $R2
  Exch $R0
  Exch
  Exch $R1
  return

 enum_processes_fail:

  Pop $R3
  Pop $R2
  Pop $R0
  Pop $R1

  Push 0
  Push 0

FunctionEnd

; Expects process ID on top of stack, returns
; filename (in device syntax) on top of stack
Function GetFilenameFromProcessId

  Exch $R0
  Push $R1
  Push $R2
  Push $R3

  !define PROCESS_QUERY_INFORMATION 0x0400
  System::Call "kernel32::OpenProcess(i ${PROCESS_QUERY_INFORMATION}, i 0, i $R0) i .R0"

  ${If} $R0 == 0

    Pop $R3
    Pop $R2
    Pop $R1
    Pop $R0
    Push ''
    return

  ${EndIf}

  StrCpy $R3 ${NSIS_MAX_STRLEN}
  System::Call "kernel32::QueryFullProcessImageName(i R0, i 0, t .R1, *i R3) i .R2"

  ${If} $R2 == 0
    ; Fallback
    System::Call "psapi::GetProcessImageFileName(i R0, t .R1, i ${NSIS_MAX_STRLEN}) i .R2"
  ${EndIf}

  ${If} $R2 == 0

    System::Call "kernel32::CloseHandle(i R0)"
    Pop $R3
    Pop $R2
    Pop $R1
    Pop $R0
    Push ''

    return

  ${EndIf}

  System::Call "kernel32::CloseHandle(i R0)"

  Pop $R3
  Pop $R2
  StrCpy $R0 $R1
  Pop $R1
  Exch $R0

FunctionEnd

; Expects process name on top of stack
; Afterwards, top of stack contains path to the process if it's running or an empty string if it is not.
Function IsProcessRunning

  Exch $R0 ; Name

  Push $R1 ; Bytes
  Push $R2 ; Array
  Push $R3 ; Counter
  Push $R4 ; Strlen
  Push $R5 ; Current process ID and image filename
  Push $R6 ; Last part of path

  StrCpy $R0 "\$R0"

  StrLen $R4 $R0
  IntOp $R4 0 - $R4

  Call EnumProcesses

  Pop $R1
  Pop $R2

  StrCpy $R3 0

  ${While} $R3 < $R1

    IntOp $R5 $R2 + $R3

    System::Call "*$R5(i .R5)"
    Push $R5

    Call GetFilenameFromProcessId

    Pop $R5

    ; Get last part of filename
    StrCpy $R6 $R5 '' $R4

    ${If} $R6 == $R0

      ; Program is running
      StrCpy $R0 $R5
      Pop $R6
      Pop $R5
      Pop $R4
      Pop $R3
      Pop $R2
      Pop $R1
      Exch $R0
      return

    ${EndIf}

    IntOp $R3 $R3 + 4

  ${EndWhile}

  Pop $R5
  Pop $R4
  Pop $R3
  Pop $R2
  Pop $R1
  Pop $R0
  Push ''

FunctionEnd

!endif ;EXECUTABLE_RUNNING_INCLUDED