简体   繁体   中英

WiX ServiceInstall - setting the service to run as the current windows user

I am installing a Windows service using WiX. How can I make the service run in the context of Windows User that runs the installer?

You need to have both the account name and password for the user you want to run the service as. I was able to accomplish this by adding a custom UI to my installer asking for a User Name and Password, and then using the supplied values for the Account and Password attributes on the ServiceInsall element.

Note that what ever account is used to run the service will need to have the Log On As Service privileged. This is not granted to users by default. I was able to use the User element from the UtilExtension schema to add this priveledge to the user. Adding the privileged to the user would only succeed if the user running the installer is an administrator.

Here's the code I used. SERVICECREDENTIALS_USERLOGIN and SERVICECREDENTIALS_PASSWORD are the properties populated from the custom UI.

<Component Id="ServiceEXE" Guid="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">
  <File Id="ServiceEXE" Name="YourService.exe" DiskId="1"
        Source="path\to\YourService.exe" KeyPath="yes" />
  <util:User Id="UpdateUserLogonAsService" UpdateIfExists="yes" CreateUser="no" Name="[SERVICECREDENTIALS_USERLOGIN]"
             LogonAsService="yes" />
  <ServiceInstall Id="ServiceInstall" Type="ownProcess" Vital="yes" Name="YourService"
                  DisplayName="Your Service" Description="Your Service description"
                  Start="auto" Account="[SERVICECREDENTIALS_USERLOGIN]" Password="[SERVICECREDENTIALS_PASSWORD]"
                  ErrorControl="normal" Interactive="no" />
  <ServiceControl Id="StartService" Start="install" Stop="both" Remove="uninstall" Name="YourService" Wait="yes" />
</Component>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM