Reputation: 23
C++ windows Service Program which should monitor all services and restart them when they crash. Any idea how to identify whether the services has crashed or properly stopped ? which winapi can be used ?
Upvotes: 0
Views: 363
Reputation: 175876
The Service Control Manager can detect a failed (correctly written) service, i.e. the death of a service process without it having returned a SERVICE_STOPPED
status or a SERVICE_STOPPED
status with a SERVICE_STATUS
containing a win32 error code - this is what the Recovery tab does.
You can access this programmatically via the ChangeServiceConfig2()
+ SERVICE_CONFIG_FAILURE_ACTIONS
and then configure it via SERVICE_FAILURE_ACTIONS
to say execute an external process.
Upvotes: 1
Reputation: 5920
There is no built-in mechanism in the Windows to determine service termination reason. SCM
will report an error if service is unable to start, however if service has been started - any failure reporting is its own business. So if service has been terminated due to the internal errors, killed through the Task Manager, or stopped wia SCM
- it will have the same status Service stopped
. If services you are trying to monitor has no error/status reporting mechanism, you will be unable to determine its termination reasons.
Upvotes: 0