Ooops
November 28, 2020, 02:53:54 AM

Author Topic: [bmx] Start/Stop and Control a Windows NT Service by andre72 [ 1+ years ago ]  (Read 427 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Start/Stop and Control a Windows NT Service
Author : andre72
Posted : 1+ years ago

Description : With these functiones you can get the state of a service:
DebugLog(ServiceStatus("", "MySql"))
The result is SERVICE_RUNNING when it is running, SERVICE_PAUSED when it is paused etc.


Code :
Code: BlitzMax
  1. 'API Constants
  2. Const SERVICES_ACTIVE_DATABASE:String = "ServicesActive"
  3. ' Service Control
  4. Const SERVICE_CONTROL_STOP:Int = $1
  5. Const SERVICE_CONTROL_PAUSE:Int = $2
  6. ' Service State - for CurrentState
  7. Const SERVICE_STOPPED:Int = $1
  8. Const SERVICE_START_PENDING:Int = $2
  9. Const SERVICE_STOP_PENDING:Int = $3
  10. Const SERVICE_RUNNING:Int = $4
  11. Const SERVICE_CONTINUE_PENDING:Int = $5
  12. Const SERVICE_PAUSE_PENDING:Int = $6
  13. Const SERVICE_PAUSED:Int = $7
  14. 'Service Control Manager object specific access types
  15. Const STANDARD_RIGHTS_REQUIRED:Int = $F0000
  16. Const SC_MANAGER_CONNECT:Int = $1
  17. Const SC_MANAGER_CREATE_SERVICE:Int = $2
  18. Const SC_MANAGER_ENUMERATE_SERVICE:Int = $4
  19. Const SC_MANAGER_LOCK:Int = $8
  20. Const SC_MANAGER_QUERY_LOCK_STATUS:Int = $10
  21. Const SC_MANAGER_MODIFY_BOOT_CONFIG:Int = $20
  22. Const SC_MANAGER_ALL_ACCESS:Int = $F003F 'STANDARD_RIGHTS_REQUIRED | SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE | SC_MANAGER_ENUMERATE_SERVICE | SC_MANAGER_LOCK or SC_MANAGER_QUERY_LOCK_STATUS | SC_MANAGER_MODIFY_BOOT_CONFIG
  23. 'Service object specific access types
  24. Const SERVICE_QUERY_CONFIG:Int = $1
  25. Const SERVICE_CHANGE_CONFIG:Int = $2
  26. Const SERVICE_QUERY_STATUS:Int = $4
  27. Const SERVICE_ENUMERATE_DEPENDENTS:Int = $8
  28. Const SERVICE_START:Int = $10
  29. Const SERVICE_STOP:Int = $20
  30. Const SERVICE_PAUSE_CONTINUE:Int = $40
  31. Const SERVICE_INTERROGATE:Int = $80
  32. Const SERVICE_USER_DEFINED_CONTROL:Int = $100
  33. Const SERVICE_ALL_ACCESS:Int = $1FF 'STANDARD_RIGHTS_REQUIRED | SERVICE_QUERY_CONFIG | SERVICE_CHANGE_CONFIG | SERVICE_QUERY_STATUS | SERVICE_ENUMERATE_DEPENDENTS | SERVICE_START | SERVICE_STOP | SERVICE_PAUSE_CONTINUE | SERVICE_INTERROGATE | SERVICE_USER_DEFINED_CONTROL
  34.  
  35. Type SERVICE_STATUS
  36.     Field dwServiceType:Int
  37.     Field dwCurrentState:Int
  38.     Field dwControlsAccepted:Int
  39.     Field dwWin32ExitCode:Int
  40.     Field dwServiceSpecificExitCode:Int
  41.     Field dwCheckPoint:Int
  42.     Field dwWaitHint:Int
  43. End Type
  44.  
  45. Extern "Win32"
  46.         Function CloseServiceHandle:Int (hSCObject:Int) = "CloseServiceHandle@4"
  47.         Function ControlService:Int (hService:Int, dwControl:Int, lpServiceStatus:Byte ptr) = "ControlService@12"
  48.         Function OpenSCManager:Int (lpMachineName:Byte Ptr, lpDatabaseName:Byte Ptr, dwDesiredAccess:Int) = "OpenSCManagerA@12"
  49.         Function OpenService:Int (hSCManager:Int, lpServiceName:Byte Ptr, dwDesiredAccess:Int) = "OpenServiceA@12"
  50.         Function QueryServiceStatus (hService:Int, lpServiceStatus:Byte ptr) = "QueryServiceStatus@8"
  51.         Function StartService:Int (hService:Int, dwNumServiceArgs:Int, lpServiceArgVectors:Int) = "StartServiceA@12"
  52. End Extern
  53.  
  54. Function ServiceStatus:Int(ComputerName:String, ServiceName:String)
  55.         Local ServiceState:SERVICE_STATUS = New SERVICE_STATUS
  56.         Local hSManager:Int = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
  57.         If hSManager <> 0
  58.                 Local hService:Int = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
  59.                 If hService<>0
  60.                         Local hServiceStatus:Int = QueryServiceStatus(hService, ServiceState)
  61.                         CloseServiceHandle hService
  62.                 Endif
  63.                 CloseServiceHandle hSManager
  64.         Endif
  65.         Return ServiceState.dwCurrentState
  66. End Function
  67.  
  68. function ServiceStart:Int(ComputerName:String, ServiceName:String)
  69.         Local hSManager:Int = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
  70.         Local res:Int
  71.         If hSManager <> 0
  72.                 Local hService:Int = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
  73.                 If hService<>0
  74.                         res = StartService(hService, 0, 0)
  75.                         CloseServiceHandle hService
  76.                 Endif
  77.                 CloseServiceHandle hSManager
  78.         Endif
  79.         Return res
  80. End Function
  81.  
  82. function ServiceStop:Int(ComputerName:String, ServiceName:String)
  83.         Local ServiceState:SERVICE_STATUS = New SERVICE_STATUS
  84.         Local hSManager:Int = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
  85.         Local res:Int
  86.         If hSManager <> 0
  87.                 Local hService:Int = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
  88.                 If hService<>0
  89.                         res = ControlService(hService, SERVICE_CONTROL_STOP, ServiceState)
  90.                         CloseServiceHandle hService
  91.                 Endif
  92.                 CloseServiceHandle hSManager
  93.         EndIf
  94.         Return res
  95. End Function
  96.  
  97. function ServicePause:Int(ComputerName:String, ServiceName:String)
  98.         Local ServiceState:SERVICE_STATUS = New SERVICE_STATUS
  99.         Local hSManager:Int = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
  100.         Local res:Int
  101.         If hSManager <> 0
  102.                 Local hService:Int = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
  103.                 If hService<>0
  104.                         res = ControlService(hService, SERVICE_CONTROL_PAUSE, ServiceState)
  105.                         CloseServiceHandle hService
  106.                 Endif
  107.                 CloseServiceHandle hSManager
  108.         EndIf
  109.         Return res
  110. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal