question

SakariasJohansson-5309 avatar image
0 Votes"
SakariasJohansson-5309 asked RLWA32-6355 commented

How to start a user service after creation?

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-apic++
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What version of Windows are you using?

What account is the Windows service supposed to use?

0 Votes 0 ·

I just tested on Win 8.1 with a console application that installed an auto-start service and then manually started it after successful installation. It started without error.

Update -
Also tested on Windows 10 20H2 -- auto-start service successfully installed and started manually without error.

Sharing the code you use to install and start a service would help us help you.

0 Votes 0 ·

I'm using windows 10. The service is supposed to run as whoever logged in, this is why I use SERVICE_USER_OWN_PROCESS. The code is in rust but the API calls are the same. The installer is running in an elevated prompt. I've stripped down some of the code that's redundant. The code still had too many characters for a post so I attached it as a file. Attached 123535-start-user-service-rust-code.txt



Long story short of what it does (in the install function).
1. get a handle to the scm.
2. creates the user service.
3. Tries to start the created user service.

And it fails on step 3.



0 Votes 0 ·
RLWA32-6355 avatar image RLWA32-6355 SakariasJohansson-5309 ·

I missed the SERVICE_USER_OWN_PROCESS flag and have now reproduced the issue on Win10.

1 Vote 1 ·

1 Answer

RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered RLWA32-6355 commented

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



before-logoff.png (26.0 KiB)
after-logoff.png (25.9 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks a lot for looking in to this. Still wonder if you can create a service from a template somehow. I will google around a bit more and try some more things.

0 Votes 0 ·
RLWA32-6355 avatar image RLWA32-6355 SakariasJohansson-5309 ·

The Microsoft documentation at per-user-services-in-windows also states that the instances are created at login and deleted at logoff.


1 Vote 1 ·