I wrote a windows service (and it runs fine). Now i have a separate app where I want to start this service from, but it seems this is not possible without administrator rights.
How would a proper solution look like that a user can start/stop the service (e.g. from a tray or application)
IMHO its bad that the application must always be started with administrator rights.
Starting a service programmatically is done with the
StartService
function. There is a comprehensive usage example also given under the title starting a service, which also shows how to:As for administrator rights, this is necessary because if just about any application could shut services down (or, more importantly, install and start new services) there would be very real and very serious security issues.
You just need to change the permissions on the service object, preferably at the same time you install it.
I'm assuming here you've already opened the service handle. You need WRITE_DAC permission.
If you also want non-admin users to be able to stop the service, add the WP right, i.e.,
SDDL codes for service rights can be found in Wayne Martin's blog entry, Service Control Manager Security for non-admins.
@Harry Johnston 's worked fine for me, in case someone wants to do this in C#:
@Harry Johnston, in addition to response.
Here is c++ builder example.