Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4945 gchristi 1
NSIS Simple Service Plugin
2
 
3
This plugin contains basic service functions like start, stop the 
4
service or checking the service status. It also contains advanced 
5
service functions for example setting the service description, changed
6
the logon account, granting or removing the service logon privilege.
7
 
8
 
9
 
10
 
11
== Short Reference ==
12
 
13
 
14
SimpleSC::InstallService [name_of_service] [display_name] [service_type] [start_type] [binary_path] [dependencies] [account] [password]
15
SimpleSC::RemoveService [name_of_service]
16
 
17
SimpleSC::StartService [name_of_service] [arguments] [timeout]
18
SimpleSC::StopService [name_of_service] [wait_for_file_release] [timeout]
19
SimpleSC::PauseService [name_of_service] [timeout]
20
SimpleSC::ContinueService [name_of_service] [timeout]
21
SimpleSC::RestartService [name_of_service] [arguments] [timeout]
22
SimpleSC::ExistsService [name_of_service]
23
 
24
SimpleSC::GetServiceDisplayName [name_of_service]
25
SimpleSC::GetServiceName [display_name]
26
SimpleSC::GetServiceStatus [name_of_service]
27
SimpleSC::GetServiceDescription [name_of_service]
28
SimpleSC::GetServiceStartType [name_of_service]
29
SimpleSC::GetServiceBinaryPath [name_of_service]
30
SimpleSC::GetServiceLogon [name_of_service]
31
SimpleSC::GetServiceFailure [name_of_service]
32
SimpleSC::GetServiceFailureFlag [name_of_service]
33
SimpleSC::GetServiceDelayedAutoStartInfo [name_of_service]
34
 
35
SimpleSC::SetServiceDescription [name_of_service] [service_description]
36
SimpleSC::SetServiceStartType [name_of_service] [start_type]
37
SimpleSC::SetServiceBinaryPath [name_of_service] [binary_path]
38
SimpleSC::SetServiceLogon [name_of_service] [account] [password]
39
SimpleSC::SetServiceFailure [name_of_service] [reset_period] [reboot_message] [command] [action_type_1] [action_delay_1] [action_type_2] [action_delay_2] [action_type_3] [action_delay_3] 
40
SimpleSC::SetServiceFailureFlag [name_of_service] [failure_actions_on_non_crash_failures]
41
SimpleSC::SetServiceDelayedAutoStartInfo [name_of_service] [delayed_autostart]
42
 
43
SimpleSC::GrantServiceLogonPrivilege [account]
44
SimpleSC::RemoveServiceLogonPrivilege [account]
45
 
46
SimpleSC::ServiceIsPaused [name_of_service]
47
SimpleSC::ServiceIsRunning [name_of_service]
48
SimpleSC::ServiceIsStopped [name_of_service]
49
 
50
SimpleSC::GetErrorMessage [error_code]
51
 
52
 
53
Parameters:
54
 
55
name_of_service - The name of the service used for Start/Stop commands and all further commands 
56
 
57
display_name - The name as shown in the service control manager applet in system control 
58
 
59
service_type - One of the following codes 
60
  1 - SERVICE_KERNEL_DRIVER - Driver service.
61
  2 - SERVICE_FILE_SYSTEM_DRIVER - File system driver service.
62
  16 - SERVICE_WIN32_OWN_PROCESS - Service that runs in its own process. (Should be used in most cases)
63
  32 - SERVICE_WIN32_SHARE_PROCESS - Service that shares a process with one or more other services. 
64
  256 - SERVICE_INTERACTIVE_PROCESS - The service can interact with the desktop. 
65
      Note: If you specify either SERVICE_WIN32_OWN_PROCESS or SERVICE_WIN32_SHARE_PROCESS, 
66
            and the service is running in the context of the LocalSystem account, 
67
            you can also specify this value. 
68
            Example: SERVICE_WIN32_OWN_PROCESS or SERVICE_INTERACTIVE_PROCESS - (16 or 256) = 272
69
      Note: Services cannot directly interact with a user as of Windows Vista. 
70
            Therefore, this technique should not be used in new code.
71
            See for more information: http://msdn2.microsoft.com/en-us/library/ms683502(VS.85).aspx          
72
 
73
start_type - one of the following codes 
74
 
75
  1 - SERVICE_SYSTEM_START - Driver scm stage start 
76
  2 - SERVICE_AUTO_START - Service auto start (Should be used in most cases)
77
  3 - SERVICE_DEMAND_START - Driver/service manual start 
78
  4 - SERVICE_DISABLED - Driver/service disabled
79
 
80
service_status - one of the following codes
81
  1 - SERVICE_STOPPED
82
  2 - SERVICE_START_PENDING
83
  3 - SERVICE_STOP_PENDING
84
  4 - SERVICE_RUNNING
85
  5 - SERVICE_CONTINUE_PENDING
86
  6 - SERVICE_PAUSE_PENDING
87
  7 - SERVICE_PAUSED
88
 
89
binary_path - The path to the binary including all necessary parameters 
90
 
91
dependencies - Needed services, controls which services have to be started before this one; use the forward slash "/" to add more more than one service
92
 
93
account - The username/account which should be used 
94
 
95
password - Password of the aforementioned account to be able to logon as a service 
96
           Note: If you do not specify account/password, the local system account will be used to run the service
97
 
98
arguments - Arguments passed to the service main function. 
99
            Note: Driver services do not receive these arguments.
100
 
101
reset_period - The time after which to reset the failure count to zero if there are no failures, in seconds. Specify 0 (INFINITE) to indicate that this value should never be reset
102
 
103
reboot_message - The message to be broadcast to server users before rebooting
104
 
105
command - The command line of the process to execute in response to the SC_ACTION_RUN_COMMAND service controller action. This process runs under the same account as the service
106
 
107
timeout - Timeout in seconds of the function
108
 
109
action_type_x - one of the following codes for the action to be performed
110
 
111
  1 - SC_ACTION_RESTART - Restart the service
112
  2 - SC_ACTION_REBOOT - Reboot the computer (Note: The service user must have the SE_SHUTDOWN_NAME privilege)
113
  3 - SC_ACTION_RUN_COMMAND - Run a command
114
 
115
action_delay_x - The time to wait before performing the specified action, in milliseconds
116
 
117
failure_actions_on_non_crash_failures - This setting determines when failure actions are to be executed
118
 
119
  1 - The failure actions executed if the status of a service is SERVICE_STOPPED but the exit code of the service is not 0
120
 
121
delayed_autostart - The delayed auto-start setting of an auto-start service
122
 
123
  1 - The service will be started after other auto-start services are started plus a short delay
124
 
125
error_code - Error code of a function
126
 
127
service_description - The description as shown in the service control manager applet in system control 
128
 
129
wait_for_file_release - Wait for file release after the service is stopped. This is useful if the binary file will be overwritten after stopping the service.
130
 
131
  1 - WAIT - Wait for file release 
132
  Note: If SERVICE_WIN32_OWN_PROCESS is used this option should be set to WAIT.
133
	If SERVICE_WIN32_SHARE_PROCESS is used this option should only be set to WAIT if the last service
134
        in the process is stopped.
135
 
136
 
137
 
138
 
139
== The Sample Script ==
140
 
141
 
142
; Install a service - ServiceType own process - StartType automatic - NoDependencies - Logon as System Account
143
  SimpleSC::InstallService "MyService" "My Service Display Name" "16" "2" "C:\MyPath\MyService.exe" "" "" ""
144
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
145
 
146
; Install a service - ServiceType interact with desktop - StartType automatic - Dependencies on "Windows Time Service" (w32time) and "WWW Publishing Service" (w3svc) - Logon as System Account
147
  SimpleSC::InstallService "MyService" "My Service Display Name" "272" "2" "C:\MyPath\MyService.exe" "w32time/w3svc" "" ""
148
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
149
 
150
; Remove a service
151
  SimpleSC::RemoveService "MyService"
152
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
153
 
154
; Start a service
155
  SimpleSC::StartService "MyService" "" 30
156
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
157
 
158
; Start a service with two arguments "/param1=true" "/param2=1"
159
  SimpleSC::StartService "MyService" "/param1=true /param2=1" 30
160
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
161
 
162
; Start a service with two arguments "-p param1" "-param2"
163
  SimpleSC::StartService "MyService" '"-p param1" -param2' 30
164
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
165
 
166
; Stop a service and waits for file release
167
  SimpleSC::StopService "MyService" 1 30
168
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
169
 
170
; Stops two services and waits for file release after the last service is stopped
171
  SimpleSC::StopService "MyService1" 0 30
172
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
173
  SimpleSC::StopService "MyService2" 1 30
174
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
175
 
176
; Pause a service
177
  SimpleSC::PauseService "MyService" 30
178
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
179
 
180
; Continue a service
181
  SimpleSC::ContinueService "MyService" 30
182
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
183
 
184
; Restart a service
185
  SimpleSC::RestartService "MyService" "" 30
186
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
187
 
188
; Restart a service with two arguments "/param1=true" "/param2=1"
189
  SimpleSC::RestartService "MyService" "/param1=true /param2=1" 30
190
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
191
 
192
; Start a service with two arguments "-p param1" "-param2"
193
  SimpleSC::RestartService "MyService" '"-p param1" -param2' 30
194
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
195
 
196
; Check if the service exists
197
  SimpleSC::ExistsService "MyService"
198
  Pop $0 ; returns an errorcode if the service doesn´t exists (<>0)/service exists (0)
199
 
200
; Get the displayname of a service
201
  SimpleSC::GetServiceDisplayName "MyService"
202
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
203
  Pop $1 ; returns the displayname of the service
204
 
205
; Get the servicename of a service by the displayname
206
  SimpleSC::GetServiceName "MyService"
207
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
208
  Pop $1 ; returns the servicename of the service
209
 
210
; Get the current status of a service
211
  SimpleSC::GetServiceStatus "MyService"
212
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
213
  Pop $1 ; return the status of the service (See "service_status" in the parameters)
214
 
215
; Get the description of a service
216
  SimpleSC::GetServiceDescription "MyService"
217
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
218
  Pop $1 ; returns the description of the service
219
 
220
; Get the start type of the service
221
  SimpleSC::GetServiceStartType "MyService"
222
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
223
  Pop $1 ; returns the start type of the service (see "start_type" in the parameters)
224
 
225
; Get the binary path of a service
226
  SimpleSC::GetServiceBinaryPath "MyService"
227
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
228
  Pop $1 ; returns the binary path of the service
229
 
230
; Get the logon user of the service
231
  SimpleSC::GetServiceLogon "MyService"
232
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
233
  Pop $1 ; returns the logon username of the service  
234
 
235
; Get the failure configuration of a service
236
  SimpleSC::GetServiceFailure "MyService"
237
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
238
  Pop $1 ; returns the reset period 
239
  Pop $2 ; returns the reboot message
240
  Pop $3 ; returns the command
241
  Pop $4 ; returns the first action (See "action_type_x" in the parameters)
242
  Pop $5 ; returns the first action delay 
243
  Pop $6 ; returns the second action (See "action_type_x" in the parameters)
244
  Pop $7 ; returns the second action delay 
245
  Pop $8 ; returns the third action (See "action_type_x" in the parameters)
246
  Pop $9 ; returns the third action delay 
247
 
248
; Get the failure flag configuration of a service
249
  SimpleSC::GetServiceFailureFlag "MyService"
250
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
251
  Pop $1 ; returns the service flag
252
 
253
; Get the delayed auto-start configuration of a service
254
  SimpleSC::GetServiceDelayedAutoStartInfo "MyService"
255
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
256
  Pop $1 ; returns the delayed auto-start configuration
257
 
258
; Set the description of a service
259
  SimpleSC::SetServiceDescription "MyService" "Sample Description"
260
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
261
 
262
; Set the starttype to automatic of a service
263
  SimpleSC::SetServiceStartType "MyService" "2"
264
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
265
 
266
; Sets the service binary path
267
  SimpleSC::SetServiceBinaryPath "MyService" "C:\MySoftware\MyService.exe"
268
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
269
 
270
; Sets the service logon to a user and grant the user the "SeServiceLogonPrivilege"
271
  SimpleSC::SetServiceLogon "MyService" "MyServiceUser" "MyServiceUserPassword"
272
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
273
  IntCmp $0 0 +1 Done Done ; If successful grant the service logon privilege to "MyServiceUser"
274
    ; Note: Every serviceuser must have the ServiceLogonPrivilege to start the service
275
    SimpleSC::GrantServiceLogonPrivilege "MyServiceUser"
276
    Pop $0 ; returns an errorcode (<>0) otherwise success (0)
277
  Done:
278
 
279
; Sets the service failure configuration - First action: Restart the service after one minute - Second action: Reboot the computer after five minutes
280
  SimpleSC::SetServiceFailure "MyService" "0" "" "" "1" "60000" "2" "300000" "0" "0" 
281
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
282
 
283
; Sets the failure flag configuration of a service
284
  SimpleSC::SetServiceFailureFlag "MyService" "1"
285
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
286
 
287
; Sets the delayed auto-start configuration of a service
288
  SimpleSC::SetServiceDelayedAutoStartInfo "MyService" "1"
289
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
290
 
291
; Remove the "SeServiceLogonPrivilege" from a user
292
  SimpleSC::RemoveServiceLogonPrivilege "MyServiceUser"
293
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
294
 
295
; Check if the service is paused
296
  SimpleSC::ServiceIsPaused "MyService"
297
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
298
  Pop $1 ; returns 1 (service is paused) - returns 0 (service is not paused)
299
 
300
; Check if the service is running
301
  SimpleSC::ServiceIsRunning "MyService"
302
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
303
  Pop $1 ; returns 1 (service is running) - returns 0 (service is not running)
304
 
305
; Check if the service is stopped
306
  SimpleSC::ServiceIsStopped "MyService"
307
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
308
  Pop $1 ; returns 1 (service is stopped) - returns 0 (service is not stopped)
309
 
310
; Show the error message if a function fails 
311
  SimpleSC::StopService "MyService" 1 30
312
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
313
  IntCmp $0 0 Done +1 +1 
314
    Push $0
315
    SimpleSC::GetErrorMessage
316
    Pop $0
317
    MessageBox MB_OK|MB_ICONSTOP "Stopping fails - Reason: $0"
318
  Done:
319
 
320
 
321
 
322
 
323
== Important Notes ==
324
- The function "SetServiceLogon" only works if the servicetype is 
325
  "SERVICE_WIN32_OWN_PROCESS".
326
- The functions "GetServiceDescription", "SetServiceDescription", "GetServiceFailure" and 
327
  "SetServiceFailure" are only available on systems higher than Windows NT. 
328
- The function "GetServiceFailureFlag", "SetServiceFailureFlag", "GetServiceDelayedAutoStartInfo" and 
329
  "SetServiceDelayedAutoStartInfo" are only available on systems higher than Windows 2003.
330
- If you change the logon of an service to a new user you have to grant him 
331
  the Service Logon Privilege. Otherwise the service cannot be started by 
332
  the user you have assigned.
333
- The functions StartService, StopService, PauseService and ContinueService uses
334
  a timeout of 30 seconds. This means the function must be executed within 30 seconds, 
335
  otherwise the functions will return an error.