File: process_running.nsh

package info (click to toggle)
filezilla 3.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 23,396 kB
  • sloc: cpp: 79,304; ansic: 36,304; sh: 10,470; makefile: 1,221; xml: 262
file content (182 lines) | stat: -rw-r--r-- 2,732 bytes parent folder | download
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
!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

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

  ${If} $R0 == 0

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

  ${EndIf}

  System::Call "psapi::GetProcessImageFileName(i R0, t .R1, i ${NSIS_MAX_STRLEN}) i .R2"

  ${If} $R2 == 0

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

    return
    
  ${EndIf}

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

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

FunctionEnd

; Expects process name on top of stack
; Gets replaced with 1 if process is running,
; 0 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

  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 $R5 $R5 '' $R4

    ${If} $R5 == $R0

      ; Program is running
      Pop $R5
      Pop $R4
      Pop $R3
      Pop $R2
      Pop $R1
      Pop $R0
      Push 1
      return

    ${EndIf}

    IntOp $R3 $R3 + 4

  ${EndWhile}

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

FunctionEnd

!endif ;EXECUTABLE_RUNNING_INCLUDED