Alaska Software Inc. - Bug in ServiceController()
Username: Password:
AuthorTopic: Bug in ServiceController()
Itai Ben-ArtziBug in ServiceController()
on Sun, 11 Nov 2018 15:58:28 -0800
It seems there is a bug in ServiceController(). The method
addController() returns .T., but no service is added.

For example:
oCtrl  := ServiceController()
oCtrl:addController( cServiceName, cServiceDesc, cEXEName, cUser,
cPassword,  , oLog )   return .T.
aScan(ServiceController():QueryAllServiceNames(), cServiceName)  
Return 0

Another example:
oCtrl  := ServiceController()
x := len(ServiceController():QueryAllServiceNames())   x=273
oCtrl:addController( cServiceName, cServiceDesc, cEXEName, cUser,
cPassword,  , oLog )   return .T.
x := len(ServiceController():QueryAllServiceNames())   x=273

Please help.
-Itai
Andreas Gehrs-Pahl
Re: Bug in ServiceController()
on Mon, 12 Nov 2018 09:54:13 -0500
Itai,

>It seems there is a bug in ServiceController(). The method
>addController() returns .T., but no service is added.

Not a bug. The :AddController() method doesn't create or add a new Service 
on your computer! It simply creates/registers a Service Controller Object 
for a Service. That Service might or might not (yet) exist on your computer, 
though.

You can actually add several Service Controller Objects for different 
Services. And if you want to get information about an existing (installed) 
Service, you might need to select/refresh that controller, using the 
:GetUpdatedControl() method.

oSC := ServiceController()
if oSC:AddController(cService, cDesc, cExeName, cUser, cPassword, , oLog)
   if (oService := oSC():GetUpdatedControl(cService)) # NIL

Now you can :Install() or :Uninstall() the oService. If the Service has been 
installed, you can :Start(), :Stop(), :Pause() and :Continue() it. You can 
also query several status ClassVars, like: :ControlsAccepted, :CurrentState, 
:ServiceType, :StartType, etc. Those ClassVars get updated for a specific 
Service when you call :GetUpdatedControl(cService).

If :AddController() returns TRUE, a new Controller was added.
If :AddController() returns FALSE, a Controller for that Service (Name) 
already existed, so it couldn't be added.

You can't add more than one Controller for the same Service at the same 
time. The different Controllers are identified/selected by their unique 
cService Names.

For a Service to be listed by :QueryAllServiceNames(), it has to be already 
installed on your computer. To install (and then start) a new Service, you 
can use something like this:

#include "Service.ch"

Procedure Main()
LOCAL oControl  := ServiceController()
LOCAL cService  := "TestService"
LOCAL cDisplay  := "My fantastic new Xbase++ Service"
LOCAL cFilePath := left(AppName(.t.), RAt('\', AppName(.t.)))
LOCAL cFileName := cFilePath + "\My_Service.exe"
   if oControl:AddController(cService, cDisplay, cFileName)
*     oControl:SetStartType(cService, SERVICE_AUTO_START)	 Default
*     oControl:SetErrorControl(cService, SERVICE_ERROR_NORMAL)	 Default
      if oControl:Install(cService)
         oControl:Start(cService)
      endif
      oControl:RemoveByName(cService)
   endif
return

If :Install() returns TRUE, you can find the Service listed by using 
oControl:QueryAllServiceNames().

Just as :AddController(cService) adds a Service Controller, rather than 
adding a Service to your system, :RemoveByName(cService) removes a Service 
Controller, rather than removing a Service from your computer. To remove a 
Server, you need to use :Uninstall(cService). To :Uninstall() a Service, it 
can't be running, so you might have to :Stop() it first.

Hope that explains it,

Andreas

Andreas Gehrs-Pahl
Absolute Software, LLC

phone: (989) 723-9927
email: Andreas@AbsoluteSoftwareLLC.com
web:   http://www.AbsoluteSoftwareLLC.com
[F]:   https://www.facebook.com/AbsoluteSoftwareLLC
Andreas Gehrs-Pahl
Re: Bug in ServiceController()
on Mon, 12 Nov 2018 09:58:25 -0500
I wrote:

>To remove a Server, you need to use :Uninstall(cService).

That was of course supposed to read:

To remove a Service, you need to use :Uninstall(cService).

Andreas

Andreas Gehrs-Pahl
Absolute Software, LLC

phone: (989) 723-9927
email: Andreas@AbsoluteSoftwareLLC.com
web:   http://www.AbsoluteSoftwareLLC.com
[F]:   https://www.facebook.com/AbsoluteSoftwareLLC
Itai Ben-ArtziRe: Bug in ServiceController()
on Mon, 12 Nov 2018 22:37:49 -0800
Thank you, Andrea.
It is not a bug, but the :Install() must be by Administrator.
I should've remembered this.
Again, thank you for the response.