Search code examples
c++windowsservicelogoff

How to capture that session logoff started from windows service


I have a service which constantly checks some application and assures that it wasn't closed. If this app closed - service launches it again.

The problem starts when user decides to log off the session. During logoff all applications are closing including the mine one. But the service is still running and constantly trying to start it again.

The questions is how to notify the service that user is going to log off and the application doesn't need to be restarted anymore? I've tried to make it using SERVICE_CONTROL_SESSIONCHANGE notification. But in accordance to MSDN they come to services when all apps already closed and logging off procedure completed. It is too late for me. Is there any way to programmatically find out that current session is in process of logging off?

My service launched under LocalSystem account.

Thanks.

p.s. I don't have the access to application source code. The goal need to be achieved without modifying it.


Solution

  • Have your service run two applications: the one it is a watchdog for, and a second one which you implement yourself. This second one can then respond to the log-off event by sending a message to the service (a la David Heffernan's answer), and the service will then know not to restart the watched job.