Well this is annoying, you would think that "ControlService()" would have a SERVICE_CONTROL_RESTART command but it doesn't. It looks like you need to take the long way around:
This code requires a second service running to restart the main service (or directly from main application, if it is running with administrative privileges).
@modoran: No it doesn't. I checked it half a dozen times, it runs as an executable off the desktop. You WILL need admin\elevated privileges but I would consider that obvious when you are working with services. I've tried this on XP and Win7 Pro, is there a platform that you've tried this on where it doesn't work?
I didn't say it didn't work, I only say that requires administrative privileges, which can be inconvenient sometimes. Read my post carefully:
or directly from main application, if it is running with administrative privileges
By "main application" I mean desktop executable, suppose that service is part of a larger program.
You WILL need admin\elevated privileges but I would consider that obvious when you are working with services.
Actually there IS a way to command a service do some work WITHOUT admin privileges, but not stopping/starting it, passing a user defined value to handler callback function registered with RegisterServiceCtrlHandler(), that's why I suggest a second service approach. http://msdn.microsoft.com/en-us/library/windows/desktop/ms683240(v=vs.85).aspx