Warning window once the user tries to stop a servi

2019-06-11 19:30发布

问题:

I'm writing a service in "C" and I'd like to display a warning window once the user tries to stop the service ( With "OK" and "Cancel" Buttons ).

Is there any specific windows API available to achieve this? Any other simple ways are there to achieve??

回答1:

No, this is not possible. Windows services are unable to interact directly with the user's desktop, so they will be unable to show a user interface of any kind.

This shouldn't really be a big deal, though. You have to have adequate permissions to stop and start a service, and by default, only Administrators have those rights. If you don't want users inadvertently stopping your service, then you should take advantage of permissions to solve that problem for you.



回答2:

You stop a service with Service control panel; your service normally doesn't have access to this control panel process, so you can't override it's UI and present a dialog asking OK/Cancel.



回答3:

I just learned the hard way that any calls made to the UI from a service will cause it to hang. MSDN does have a page here on work arounds.



回答4:

If you don't want anybody to be able to stop the service, just tell the service manager that you don't accept stop messages. You could instead provide an application that stops the service (using some form of IPC) and that application could present as many warning messages as you wanted.

On the other hand, this will annoy your users, and it is unlikely to be necessary. By default, only administrators can stop services, and people are unlikely to try to stop your service without a good reason.