Adjusting a services "CanStop" on the fly?

G

gregory_may

According to this article, I cant change "CanStop" on the fly!!!

http://msdn.microsoft.com/library/d...erviceprocessservicebaseclasscanstoptopic.asp

So, how can I enable/disable the ability for a user to kill my service (Via
Task Manager) on the fly?

I would prefer not to tweak the regestry settings that permit Task manager
from be available to a user. I have not had good luck down this path & task
manager needs to be available for other reasons.

I was hoping for a way to disable someone from killing my service if they
dont have the Administrative Privledge.

If I understand things correctly, the thread that a user will be running
under is not known at the time the service starts. Users can log off/On
with different privledges.

I was also unsuccessfull at navigating the windows station to deturmine who
is trying to kill the service. This article covers all the functions
relating to figuring out who is logged in. With fast user switching under
XP, several users can actually be logged into the PC at one time.
http://msdn.microsoft.com/library/d...base/window_station_and_desktop_functions.asp
 
G

gregory_may

Maybe a good follow up question is if someone kills my service in Task
Manager if the OnStop event gets fired?

I am really tring to protect against a TaskManager Hard Kill, if they stop
the service via the Control Manager, thats ok.
 
J

Jay B. Harlow [MVP - Outlook]

Gregory,
I was also unsuccessfull at navigating the windows station to deturmine
who is trying to kill the service.
Which is what I stated in my other post, I really don't think you can
determine who is trying to kill the service. This would really be a Win32
question, you probably want to ask in a Win32 newsgroup!
So, how can I enable/disable the ability for a user to kill my service
(Via Task Manager) on the fly?
Again, as you found out, you can't change the setting on the fly, as it
doesn't really make sense to. What should the setting be when there is an
Admin logged in & a Non-Admin logged in? Don't forget other services can
control your service & each service is "logged in" to Windows...


I have not tried it, what happens if the OnStop method throws an exception?
Does the service still stop or will the stopping of the service be aborted?
I know if OnStart throws an exception the starting of the service is
aborted. I just don't know about the stopping. An exception may abort the
stopping of the service, keeping the service going, or it may simply keep
stopping the service... Of course you would still need to figure out who
actually made the request to decide if you should throw the exception...

It appears the RegisterServiceCtrlHandlerEx (a Win32 API) supports a
SERVICE_CONTROL_SESSIONCHANGE event, which notifies a service of session
change events. You could possibly use this event to be notified when the
"current user" changes from an Admin user & a non Admin user, unfortunately
ServiceBase hides the details of RegisterServiceCtrlHandler &
RegisterServiceCtrlHandlerEx from you... Again I would recommend you ask in
a Win32 newsgroup on the specifics of RegisterServiceCtrlHandler &
RegisterServiceCtrlHandlerEx...

Of course I may simply be miss reading what SERVICE_CONTROL_SESSIONCHANGE
really telling me...

Hope this helps
Jay
 
G

gregory_may

Thanks!

I have been thinking this over pretty hard. I am willing to just make the
service "CanStop" for everyone .... as long as Task Manager cant kill it
(This is what I really want to fix).

I posted this question to the .Net framework, but I will repost in win32
(Which win32 do you suggest, none of them seem to be a good fit for this
question).

Thanks!
 
J

Jay B. Harlow [MVP - Outlook]

Gregory,
When I can't find a specific group, I normally go for the most general
group, general win32 programming in this case that is, probably
microsoft.public.win32.programmer.kernel

The danger of course is someone may suggest go to the .NET group, so try to
"hide" the fact its a .NET question...

Hopefully someone in the general group will redirect you to a more specific
group if there is one...
as long as Task Manager cant kill it (This is what I really want to fix).

I would think Task Manager does the Win32 equivalent of Process.Kill rather
then the more polite ServiceController.Stop or Process.CloseMainWindow. In
other words I would not expect Task Manager to honor the CanStop property,
rather it simply does: Ready, Aim, Fire!

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top