I have a custom windows service with my own register handler, how can I prevent it from stopping when it receives the stop command from the services manager ? I established a pipe communication between the service and a GUI and I want to stop it only from the GUI, when I send the windows service the message "stop" not when I try to stop it from windows service manager. I am working in C#
-
2What about when the machine shuts down? Does the command to stop the service not come from services manager? – paparazzo Jun 20 '12 at 17:45
-
1Doesn't that sort of defeat the purpose of using a Windows Service? – Joel Etherton Jun 20 '12 at 17:46
-
4Well I don't care about the purpose, I just want to do it. – AlexandruC Jun 20 '12 at 17:46
-
You are right about shutting down, didn't thought about that. – AlexandruC Jun 20 '12 at 17:47
-
The thing is that I don't want a user to stop it, even if it is admin. How can I do this? – AlexandruC Jun 20 '12 at 17:48
-
If you are using a newer version of windows you do have some control over shutdown...http://channel9.msdn.com/posts/Application-Restart-and-Recovery-on-Windows-7-in-Managed-Code – Gene S Jun 20 '12 at 17:51
-
This should not be possible. As a user I wanna be able to stop any service. – MrFox Nov 20 '14 at 16:12
2 Answers
Does this answer your question?
http://bytes.com/topic/c-sharp/answers/444965-prevent-service-stopping
Set the CanStop property to false. Then you will need an application that the user can use to request a stop. This app would then communicate with your service (via remoting, perhaps) and if the condition is permitted, stop itself. You should be able to do this by dynamically setting 'CanStop' to true, then connecting to yourself with the ServiceController class and issuing the 'Stop' command.

- 2,735
- 3
- 25
- 35
-
Agree with your comment on cannot prevent the stop command within the OnStop. I deleted my answer. – paparazzo Jun 20 '12 at 18:11
-
As an alternative you could simply secure the service settings (modify the service DACL) such that only a special user account (and perhaps SYSTEM) has the rights to stop the service and install this special local user account during installation.
Your control application can then use the standard Service Control Manager functions with this special user's security token and everything will work as expected without having to do anything unusual.

- 146
- 4