views:

1037

answers:

2

Ok so part two of I have no will power experiment is:

Summary Question - Is there a way to set the CanStop property on a windows service dynamically?

Whole Spiel - I have a service that is currently checking and killing processes (IE Games) I have told it to if it's day I'm not allowed. Great. I set the CanStop to false so that I can't just kill the service if I give into the addiction. I have a program that will have a password check (Someone else enters the password) that will stop the service if the password is correct. (If I have serious withdrawals) Problem is using the ServiceController class.

Far as I can tell, ServiceController just is a decorator (yah design patern guess) and so I have no way to get at the actual service it represents. First attempt was Property Info, but I was too dumb to realize what that would be pointless. Second was Field Info because I thought there might be a private field that "represents" the service. As you might guess, both failed.

Any ideas?

EDIT 1 I'd like to avoid having the CanStop value somewhere I can get to it easily like a config file or registry. So I am attempting, though not successfully, to make this completely handled in program.

New (Failed) Attempts:

ManagementObject service; ManagementBaseObject stopService;

service = new ManagementObject("Win32_Service.Name='StopProgram'");

stopService = service .InvokeMethod("StopService", null, null);

Not sure this did anything. I assume it couldn't stop because of the CanStop situation.

A: 

Rather than trying to directly access and control the Service, could you set a flag somewhere, (like the registry or a file), that is then checked by your service before it executes the Event you're trying to control.

Timothy Carter
+1  A: 

The "CanStop" is a attribute of the services registration in the windows service control manager. You can't change it mid-stride.

And, of course, if you're smart enough to write your own service then you're smart enough to bring up task-man and simply kill the service process. CanStop will not prevent you from pulling the rug out from under the service. CanStop only tells the service control manager not to send "Stop" commands to the service.

If you want to allow something to pass then use a global event to enable/disable the checking the service does -- or just remove the games from the PC! :-)

WaldenL
Foiled by the process kill. I didn't even thing of that. And yes I could remove the game, but this was more of a learning thing.
Programmin Tool