Search code examples
c++winapiservicenotificationsshutdown

Correct way to register for pre-shutdown notification from C++


I write a local service application using C++ and I can't find the correct way of registering for a pre-shut-down notification (for OS later than Windows XP). I believe that SERVICE_CONTROL_PRESHUTDOWN notification has been added since Vista, but when you call SetServiceStatus do we need to specify:

dwServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_PRESHUTDOWN;

or

dwServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PRESHUTDOWN;

Solution

  • You cannot accept both a shutdown and a preshutdown if your service is correctly coded. The documentation explicitly states this.

    From http://msdn.microsoft.com/en-us/library/windows/desktop/ms683241(v=vs.85).aspx:

    Referring to SERVICE_CONTROL_PRESHUTDOWN:

    A service that handles this notification blocks system shutdown until the service stops or the preshutdown time-out interval specified through SERVICE_PRESHUTDOWN_INFO expires.

    In the same page, the section about SERVICE_CONTROL_SHUTDOWN adds:

    Note that services that register for SERVICE_CONTROL_PRESHUTDOWN notifications cannot receive this notification because they have already stopped.

    So, the correct way is to set the dwControlsAccepted to include either SERVICE_ACCEPT_SHUTDOWN or SERVICE_ACCEPT_PRESHUTDOWN, depending on your needs, but not to both at the same time.

    But do note that you probably want to accept more controls. You should always allow at least SERVICE_CONTROL_INTERROGATE, and almost certainly allow SERVICE_CONTROL_STOP, since without the latter the service cannot be stopped (e.g. in order to uninstall the software) and the process will have to be forcibly terminated (i.e. killed).