Reputation: 3637
How can i prompt for password and user when someone tries to stop a windows service?
Upvotes: 0
Views: 2557
Reputation: 36348
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.
Upvotes: 1
Reputation: 591
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.
Upvotes: 2
Reputation: 31721
It cannot be done.
Services are meant to run outside the user experiance and do not handle GUI interactions. This is something that is left up to the operating system to allow or disallow a user from stopping a service.
Upvotes: 2