Proper registration method for pre-warning from C ++

I am writing a local service application using C ++, and I cannot find the correct registration method for pre-disabling notification (for OS later than Windows XP). I believe the SERVICE_CONTROL_PRESHUTDOWN notification has been added since Vista, but when you call SetServiceStatus, we need to specify:

dwServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_PRESHUTDOWN; 

or

 dwServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PRESHUTDOWN; 
+4
source share
3 answers

You cannot accept either shutdown or pre-shutdown if your service is correctly encoded. 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 : "The service that processes this notification blocks the system from shutting down until the service shuts down or the wait interval specified by SERVICE_PRESHUTDOWN_INFO expires."

On the same page, the SERVICE_CONTROL_SHUTDOWN section adds: "Please 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 dwControlsAccepted to enable SERVICE_ACCEPT_SHUTDOWN or SERVICE_ACCEPT_PRESHUTDOWN, depending on your needs, but not both at the same time.

But 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 (for example, to remove software), and the process must be forcibly terminated (i.e. killed).

+6
source

As the commentators noted above, you need to select either SERVICE_ACCEPT_SHUTDOWN or SERVICE_ACCEPT_PRESHUTDOWN (Vista or later). If you are using SERVICE_ACCEPT_PRESHUTDOWN, you will need to register their service in the SCM, using RegisterServiceCtrlHandlerEx instead RegisterServiceCtrlHandler, otherwise you will not receive the provisional arrest notice. The handler prototype also changes from Handler to HandlerEx .

Another thing to keep in mind is that processing clean shutdown events is limited to 5 seconds in Windows Server 2012 (and presumably Windows 8), 12 seconds in Windows 7 and Windows Server 2008, 20 seconds in Windows XP your service will be killed while stopping. For this reason, you may need prior notice. You can change this in \\ HKLM \ SYSTEM \ CurrentControlSet \ Control \ WaitToKillServiceTimeout.

+2
source

These two notifications seem different since I get it from the documentation. If you really need your service to receive a pre-launch notification, you should go with: dwServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_PRESHUTDOWN; But if you also want your service to receive disconnect notifications, you should go with your second option.

0
source

Source: https://habr.com/ru/post/1445299/


All Articles