Why on earth would you want that? But no, there is no direct way to do it, since Windows Services aren't tied to the Windows GUI.
–
QuintiumJun 7 '12 at 18:54

1

It is assumed that anyone with the privileges required to stop a service has administrative controls and can pretty much do whatever they want. No additional password should be necessary.
–
Mark RansomJun 7 '12 at 19:01

Going about this the wrong way. Services should be locked down to admins only. If someone is the admin on their machine they should be allowed to start and stop whatever they want.
–
Tony318Jun 7 '12 at 19:05

I want do this as a windows service project. Isn't there any solution to this? in c#
–
Alexandru C.Jun 7 '12 at 19:08

3 Answers
3

Ordinarily, there's no reason to do this. By default, only administrators can stop a service, and if the service can be stopped at all it makes no sense to ask an administrator for a password to do so: they're an administrator, so by definition they're entitled to do anything.

The one scenario that makes sense is if you want ordinary users to be able to stop the service if they know the password. That way, you can let someone stop the service without giving them administrative rights to the computer. (Even then, in most cases it would be simpler to change the permissions on the service to allow the user(s) in question the right to stop the service; but perhaps, for example, you want users to be have to phone a helpdesk to be given the password.)

The secret to making this work is that the service is entitled to stop itself for any reason without having received a stop request from the operating system. So you can just write a program that the users can run if they want to stop the service. The program accepts the password and sends it to the service over some form of IPC, such as a named pipe. If the password is correct, the service stops.

You could also configure the service so that it doesn't accept stop requests, in which case an administrator would also need the password in order to stop the service nicely. But that wouldn't stop them from stopping the service by killing the service process, or uninstalling the service and rebooting the computer.

It cannot be done directly from the service. However, the service can be managed by another application if the service is set to interact with the desktop. So you could create a second application with a GUI that monitored the service. The service could set some value in shared state file when the services 'Stopping' event fires and wait until the monitoring app writes a confirmation value in the state file. Maybe not what you are looking for exactly but I think you could get the result that you want.

If a user has the rights to stop the service or the monitor they can. That might be harder to stop. But if the service periodically checks to make sure the monitoring app is running and starts it if its not... that gets you half way there.
–
Dusty LauJun 7 '12 at 20:13

1

There's no need for a GUI process to be running all the time, just have the user launch the process if they want to stop the service.
–
Harry JohnstonJun 7 '12 at 20:20