How to start a user service after creation?

Sakarias Johansson 21 Reputation points
2021-08-16T09:48:55.61+00:00

I've been trying to create an app that installs a user service and starts it without having to log out and then log in.
I do not understand how to achieve this.

I create a service by calling CreateServiceW the following arguments set (some args omitted).
dwDesiredAccess set to GENERIC_ALL | SERVICE_START
dwServiceType set to SERVICE_USER_OWN_PROCESS
dwStartType set to SERVICE_AUTO_START

This creates the service and I can find it in the Services tab in the task manager but not in Services.
If I try to start the service with StartServiceW I get "Error 1058: The service cannot be started, either because it is disabled or because it has no enabled devices associated with it.". If I log out and then log in I get yet another service (that is running) with the same name but with an underscore and some random chars appended to the name.

I get that the service I first created wasn't really the user service and the service that popped in after logging in and out again is the actual user service.
I've tried to find more information about user services but it's very sparse.

So how can I start a user service manually after creation with the win api without having to log out and in again?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,448 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,561 questions
{count} votes

Accepted answer
  1. RLWA32 40,946 Reputation points
    2021-08-16T14:15:04.173+00:00

    Some more information about per-user services is at per-user-services-in-windows-info-and-configuration. Note that Win 10 creates the service instance at logon and destroys it at logoff. Since creating the template doesn't create the actual service instance it appears that a logoff/logon sequence is required.

    Before logoff/logon
    123597-before-logoff.png

    After logoff/logon
    123638-after-logoff.png


0 additional answers

Sort by: Most helpful