tags:

views:

331

answers:

3

I'm stopping a service in my application wanted to know what is the usage of ExitProcess and if I should use it

+2  A: 

Look into the OpenSCManager(), OpenService() and ControlService() Windows API calls. Note that your program may not have the necessary permissions to call these, so elevation may be necessary - see Service Security and Access Rights for further information.

There is also an example how to stop a service.

mghie
A: 

ExitProcess is for ending the process itself (much like ExitThread ends a thread). This is used to end a process (program or DLL), though Microsoft don't recommend it for ending a DLL.

If you are trying to stop yourself (you are the process), you can use ExisProcess, though I would recommend a cleaner shutdown, to make sure everything is cleanly stopped. ExitProcess, like ExitThread, stops without unwinding the stack, so no destructors are called.

From within a service, you stop based on an external signal. You could respond to that by calling ExitProcess, but it would probably be better to have some form of shutdown that closes anything necessary and logs this.

Simon Parker
+1  A: 

You should never need to use ExitProcess() to stop a service. In fact, you should never need to use ExitProcess() at all.

Services are deeply intertwined with the SCM, and if a service that it thinks should be running just vanishes it will take some action to repair it. In extreme cases, it will force the system to reboot.

The correct way to stop a service is to use the documented API to ask the SCM to ask the service to stop. It often takes several seconds for this process to complete as the service itself usually needs to a clean shutdown after asking its worker threads to finish up and halt.

The privileges required to interact with the SCM are less dangerous than that required to end an arbitrary process, but neither is usually granted outside of the Administrators group.

Edit: A comment asked about stopping a service from inside itself.

That can be a tough call, especially if the service is in some kind of unfortunate state. The service and the SCM absolutely have to agree that the service is stopping, or the SCM will take the recovery action that was configured for the service.

I do have a complete implementation of a service that might serve as an alternative point of view for how to handle some of these things. It is LuaService and is a framework that allows a (single worker thread) service to be implemented in pure Lua aside from the LuaService executable itself. Its reference manual attempts to fully document the internals, as well as document some of the details of a service's lifetime that are otherwise documented through the interaction of various articles on MSDN.

RBerteig
What do you suggest for stopping the service from within the service (when needed)?I am using CONTROL_SERVICE( with SERVICE_CONTROL_STOP but I want to stop it evem if it is stuck in stoppping...
sofr
I updated my answer a bit. I'm not certain I've addressed your specific issue, but I'm also not certain it can be addressed in any case.
RBerteig