NSIS Simple Service Plugin: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
mNo edit summary
mNo edit summary
Line 136: Line 136:
   SimpleSC::ServiceIsPaused "MyService"
   SimpleSC::ServiceIsPaused "MyService"
   Pop $0; return error(1)/success(0)
   Pop $0; return error(1)/success(0)
   Pop $1; return 0 (service is paused) - return 1 (service is not paused)
   Pop $1; return 1 (service is paused) - return 0 (service is not paused)


; Check if the service is running
; Check if the service is running
   SimpleSC::ServiceIsRunning "MyService"
   SimpleSC::ServiceIsRunning "MyService"
   Pop $0; return error(1)/success(0)
   Pop $0; return error(1)/success(0)
   Pop $1; return 0 (service is running) - return 1 (service is not running)
   Pop $1; return 1 (service is running) - return 0 (service is not running)


; Check if the service is stopped
; Check if the service is stopped
   SimpleSC::ServiceIsStopped "MyService"
   SimpleSC::ServiceIsStopped "MyService"
   Pop $0; return error(1)/success(0)
   Pop $0; return error(1)/success(0)
   Pop $1; return 0 (service is stopped) - return 1 (service is not stopped)
   Pop $1; return 1 (service is stopped) - return 0 (service is not stopped)
</highlight-nsis>
</highlight-nsis>



Revision as of 17:22, 7 May 2007

This plugin contains basic service functions like start, stop the service or checking the service status. It also contains advanced service functions for example setting the service description, changed the logon account, granting or removing the service logon privilege.


This plugin is using the MPL License or alternatively the LGPL License.


Links

NSIS_Simple_Service_Plugin_1.0.zip (36 KB) The ZIP file containing a precompiled plugin DLL (to be saved in NSIS' plugin directory) and the sources.

Short Reference

SimpleSC::Install [name_of_service] [display_name] [start_type] [service_commandline] [dependencies] [account] [password]
SimpleSC::Remove [name_of_service]
 
SimpleSC::StartService [name_of_service] 
SimpleSC::StopService [name_of_service] 
SimpleSC::PauseService [name_of_service] 
SimpleSC::ContinueService [name_of_service]
SimpleSC::RestartService [name_of_service]
SimpleSC::ExistsService [name_of_service]
 
SimpleSC::GetServiceDisplayName [name_of_service]
SimpleSC::GetServiceName [display_name]
SimpleSC::GetServiceStatus [name_of_service]
 
SimpleSC::SetServiceDescription [name_of_service] [service_description]
SimpleSC::SetServiceStartType [name_of_service] [start_type]
SimpleSC::SetServiceLogon [name_of_service] [account] [password]
 
SimpleSC::GrantServiceLogonPrivilege [account]
SimpleSC::RemoveServiceLogonPrivilege [account]
 
SimpleSC::ServiceIsPaused [name_of_service]
SimpleSC::ServiceIsRunning [name_of_service]
SimpleSC::ServiceIsStopped [name_of_service]


Parameters:

  • name_of_service - the name of the service used for Start/Stop commands and all further commands
  • display_name - the name as shown in the service control manager applet in system control
  • start_type - one of the following codes
    • 0 - driver boot stage start
    • 1 - driver scm stage start
    • 2 - service auto start
    • 3 - driver/service manual start
    • 4 - driver/service disabled
  • service_commandline - the path to the binary including all necessary parameters
  • dependencies - needed services, controls which services have to be started before this one
  • account - the username/account which should be used
  • password - password of the aforementioned account to be able to logon as a service
    • If you do not specify account/password, the local system account will be used to run the service
  • service_description - the description as shown in the service control manager applet in system control

The Sample Script

; Install a service - StartType automatic - NoDependencies - Logon as System Account
  SimpleSC::InstallService "MyService" "My Service Display Name" "2" "C:\MyPath\MyService.exe" "" "" ""
  Pop $0 ; return error(1)/success(0)
 
; Remove a service
  SimpleSC::RemoveService "MyService"
  Pop $0 ; return error(1)/success(0)
 
; Start a service
  SimpleSC::StartService "MyService"
  Pop $0 ; return error(1)/success(0)
 
; Stop a service
  SimpleSC::StopService "MyService"
  Pop $0 ; return error(1)/success(0)
 
; Pause a service
  SimpleSC::PauseService "MyService"
  Pop $0 ; return error(1)/success(0)
 
; Continue a service
  SimpleSC::ContinueService "MyService"
  Pop $0 ; return error(1)/success(0)
 
; Restart a service
  SimpleSC::RestartService "MyService"
  Pop $0 ; return error(1)/success(0)
 
; Check if the service exists
  SimpleSC::ExistsService "MyService"
  Pop $0 ; return service exists(1)/service doesn´t exists (0)
 
; Get the displayname of a service
  SimpleSC::GetServiceDisplayName "MyService"
  Pop $0 ; return error(1)/success(0)
  Pop $1 ; return the displayname of the service
 
; Get the servicename of a service by the displayname
  SimpleSC::GetServiceName "MyService"
  Pop $0 ; return error(1)/success(0)
  Pop $1 ; return the servicename of the service
 
; Get the current status of a service
  SimpleSC::GetServiceStatus "MyService"
  Pop $0 ; return error(1)/success(0)
  Pop $1 ; return the status of the service
  ; Valid values are:
  ; 1 - SERVICE_STOPPED
  ; 2 - SERVICE_START_PENDING
  ; 3 - SERVICE_STOP_PENDING
  ; 4 - SERVICE_RUNNING
  ; 5 - SERVICE_CONTINUE_PENDING
  ; 6 - SERVICE_PAUSE_PENDING
  ; 7 - SERVICE_PAUSED
 
; Set the description of a service (Not supported on Windows NT 4.0)
  SimpleSC::SetServiceDescription "MyService" "Sample Description"
  Pop $0 ; return error(1)/success(0)
 
; Set the starttype to automatic of a service
  SimpleSC::SetServiceStartType "MyService" "2"
  Pop $0 ; return error(1)/success(0)
 
; Sets the service logon to a user and grant the user the "SeServiceLogonPrivilege"
  SimpleSC::SetServiceLogon "MyService" "MyServiceUser" "MyServiceUserPassword"
  Pop $0; return error(1)/success(0)
  StrCmp $0 +1 Done ; If successful grant the service logon privilege to "MyServiceUser"
    ; Note: Every serviceuser must have the ServiceLogonPrivilege to start the service
    SimpleSC::GrantServiceLogonPrivilege "MyServiceUser"
    Pop $0; return error(1)/success(0)
    Done:
 
; Remove the "SeServiceLogonPrivilege" from a user
  SimpleSC::RemoveServiceLogonPrivilege "MyServiceUser"
  Pop $0; return error(1)/success(0)
 
; Check if the service is paused
  SimpleSC::ServiceIsPaused "MyService"
  Pop $0; return error(1)/success(0)
  Pop $1; return 1 (service is paused) - return 0 (service is not paused)
 
; Check if the service is running
  SimpleSC::ServiceIsRunning "MyService"
  Pop $0; return error(1)/success(0)
  Pop $1; return 1 (service is running) - return 0 (service is not running)
 
; Check if the service is stopped
  SimpleSC::ServiceIsStopped "MyService"
  Pop $0; return error(1)/success(0)
  Pop $1; return 1 (service is stopped) - return 0 (service is not stopped)

Important Notes

  • The functions "GetServiceDisplayName" or "SetServiceDisplayName" are only available on systems higher than Windows NT.
  • If you change the logon of an service to a new user you have to grant him the Service Logon Privilege. Otherwise the service cannot be started by the user you have assigned.
  • If you have any suggestions, comments or questions please mail me: mailto:rainer@speed-soft.de