
How I can start, stop and pause an existing service without the pbosl libraries?
Thanks

Code: Select all
;/////////////////////////////////////////////////////////////////////////////////
;***COMate***  COM automation through iDispatch.
;*===========
;*
;*WMI demo - services.
;/////////////////////////////////////////////////////////////////////////////////
IncludePath "..\..\"
XIncludeFile "COMate.pbi"
Enumeration
  #StopService
  #PauseService
  #StartService
EndEnumeration
;Returns #True if successful.
Procedure Set_Service(serviceName$, action = #StopService)
  Protected result, strComputer.s, objWMIService.COMateObject, service.COMateObject 
  Protected colServices.COMateEnumObject 
  strComputer = "." 
  objWMIService = COMate_GetObject("winmgmts:\\" + strComputer + "\root\cimv2", "") 
  If objWMIService 
    colServices = objWMIService\CreateEnumeration("ExecQuery('Select * FROM Win32_Service WHERE Name = $0027" + serviceName$ + "$0027')") 
    If colServices
      service = colServices\GetNextObject() 
      If service
        Select action
          Case #StopService
            If SUCCEEDED(service\Invoke("StopService"))
              result = #True
            EndIf
          Case #PauseService
            If SUCCEEDED(service\Invoke("PauseService"))
              result = #True
            EndIf
          Case #StartService
            If SUCCEEDED(service\Invoke("StartService"))
              result = #True
            EndIf
        EndSelect
        service\Release() 
      EndIf 
      colServices\Release() 
    EndIf 
    objWMIService\Release()  
  EndIf 
  ProcedureReturn result
EndProcedure 
Set_Service("Apache2", #StartService)
PBOSL works well but I want to reduce the dependence of my work from external libraries.srod wrote:You can use WMI via COMate :
Alternatively, you can also use 'Active Directory' (again with COMate), although the code would be very similar to the above.Code: Select all
;///////////////////////////////////////////////////////////////////////////////// ;***COMate*** COM automation through iDispatch. ;*=========== ;* ;*WMI demo - services. ;///////////////////////////////////////////////////////////////////////////////// IncludePath "..\.." XIncludeFile "COMate.pbi" Enumeration #StopService #PauseService #StartService EndEnumeration ;Returns #True if successful. Procedure Set_Service(serviceName$, action = #StopService) Protected result, strComputer.s, objWMIService.COMateObject, service.COMateObject Protected colServices.COMateEnumObject strComputer = "." objWMIService = COMate_GetObject("winmgmts:\" + strComputer + "\root\cimv2", "") If objWMIService colServices = objWMIService\CreateEnumeration("ExecQuery('Select * FROM Win32_Service WHERE Name = $0027" + serviceName$ + "$0027')") If colServices service = colServices\GetNextObject() If service Select action Case #StopService If SUCCEEDED(service\Invoke("StopService")) result = #True EndIf Case #PauseService If SUCCEEDED(service\Invoke("PauseService")) result = #True EndIf Case #StartService If SUCCEEDED(service\Invoke("StartService")) result = #True EndIf EndSelect service\Release() EndIf colServices\Release() EndIf objWMIService\Release() EndIf ProcedureReturn result EndProcedure Set_Service("Apache2", #StartService)
Just out of curiosity, what's wrong with PBOSL ?
Code: Select all
status.SERVICE_STATUS
servicename$ = "Spooler"
hmanager = OpenSCManager_(0, 0, #SC_MANAGER_ALL_ACCESS)
hservice = OpenService_(hmanager, @servicename$, #SERVICE_ALL_ACCESS)
ControlService_(hservice, #SERVICE_CONTROL_INTERROGATE, @status)
If status\dwCurrentState = #SERVICE_RUNNING 
	ControlService_(hservice, #SERVICE_CONTROL_STOP, @status)
ElseIf status\dwCurrentState = #SERVICE_STOPPED
	StartService_(hservice, 0, 0)
EndIf
CloseServiceHandle_(hservice)
CloseServiceHandle_(hmanager)
Since I am new to PB can someone explain what I am missing in my knowledge of PB. I see this code works but fail to understand how PB knows what to do with OpenScManager_(), ControlService_() etc.maw wrote:It's very easy to control services using API actually. The following snippet should get you on your way:
Please note that the above snippet will stop the print spooler service if it's running and start it if it's not running.Code: Select all
status.SERVICE_STATUS servicename$ = "Spooler" hmanager = OpenSCManager_(0, 0, #SC_MANAGER_ALL_ACCESS) hservice = OpenService_(hmanager, @servicename$, #SERVICE_ALL_ACCESS) ControlService_(hservice, #SERVICE_CONTROL_INTERROGATE, @status) If status\dwCurrentState = #SERVICE_RUNNING ControlService_(hservice, #SERVICE_CONTROL_STOP, @status) ElseIf status\dwCurrentState = #SERVICE_STOPPED StartService_(hservice, 0, 0) EndIf CloseServiceHandle_(hservice) CloseServiceHandle_(hmanager)
Okay so I found part of the answer in another message but I do not know where in the documentation this is mentioned.PB will instantly recognize Win API constants if you preceed them with the "#" (pound) character, just as it instantly recognizes Win API procedures if you trail them with a "_" (underscord) character.
Code: Select all
status.SERVICE_STATUS
servicename$ = "Fax"
hmanager = OpenSCManager_(0, 0, #SC_MANAGER_ALL_ACCESS)
hservice = OpenService_(hmanager, @servicename$, #SERVICE_ALL_ACCESS)
ControlService_(hservice, #SERVICE_CONTROL_INTERROGATE, @status)
If status\dwCurrentState = #SERVICE_RUNNING 
	ControlService_(hservice, #SERVICE_CONTROL_STOP, @status)
ElseIf status\dwCurrentState = #SERVICE_STOPPED
	StartService_(hservice, 0, 0)
EndIf
CloseServiceHandle_(hservice)
CloseServiceHandle_(hmanager)
 So it's not the code above causing it. Sorry. But why can't I start/stop services as admin all of a sudden?
  So it's not the code above causing it. Sorry. But why can't I start/stop services as admin all of a sudden?