I have a windows service running in my own service framework. Currently, the service can be stopped by using SCM API by sending SERVICE_CONTROL_STOP control code. What I want to do is that the service will ignore the control code unless there is a registry key set (that will be used as a flag showing authorized stop) so that the service won't stop for an authorized stops.

In prototyping, I found that only the first stop attempt (from sc stop) was ignored in HandlerEx function that I registered with (RegisterServiceCtrlHandlerEx API), but the second attempt went through because the service HandlerEx function didn't even get SERVICE_CONTROL_STOP control code on second attempt, and the win32 API (StartServiceCtrlDispatcher) returned although my service hasn't set the service status to SERVICE_STOPPED.

So, basically, the design works only for the first time. I wonder why the service didn't even get the stop control code on second attempt, and the API StartServiceCtrlDispatcher returned. I don't understand why it happened, and why the behaviors are different in first attempt and second attempt. Any explaination is highly appreciated. Any better approaches are also appreciated.

Thanks in advance.

Not stopping your service is a bit unfriendly, do you properly handle getting stopped if windows is shutting down?

I suspect the reason you don't get called a second time is because something you're doing in the first event causes you to be removed from the handler chain.

Confirm that any value you return is an expected and 'normal' value.
If you are passed any data structures that you modify, make sure you are setting the proper values.

If everything appears correct, it may be that you have to re-add your handler every time it is called. (It doesn't make a lot of sense, but it wouldn't be the first time I've seen something like it.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.