I have an old Windows Service written in Borland C++ Builder that I need to extend so that it can shutdown itself under certain conditions.
If I shutdown the service manually via the service control manager, it shuts down properly without any problems. So I thought, calling this->DoShutdown();
would be sufficient (this
being an instance derived from TService
). But this leaves the service in the state "Shutting down...". I could call ExitProcess
afterwards, but this creates an entry in the event log that the service has been shut down unexpectedly.
So what is the proper way to make a Borland C++ Windows service shut down itself?
DoShutdown()
is called by TService
when it receives a SERVICE_CONTROL_SHUTDOWN
request from the SCM while Windows is being shut down. DoShutdown()
is not intended to be called directly in user code.
The easiest way to have your service terminate itself is to call its Controller()
method (either directly, or via its global ServiceController()
function), passing either SERVICE_CONTROL_STOP
or SERVICE_CONTROL_SHUTDOWN
in the CtrlCode
parameter. Let the service handle the request as if it had come from the SCM, so it can act accordingly.