title: Wix: Stopping a Windows Service on uninstall
body: <p>When I uninstall my service I get the error where it says I have to stop such and such service before uninstalling. This is unsatisfactory - the uninstaller should automatically stop it.</p>
  
  <p>I found a blog or newsgroup posting on this months ago and got it to work properly, but now it has stopped working for me. And I don't have a link to the post... maybe someone else know where it is? :) I guess I changed some subtle thing and it stopped working. I find Wix extremely difficult to troubleshoot.</p>
  
  <p>I am using the following include to fetch the property X_ WIN_ SERVICE_ NAME (sorry I don't know how to avoid _ escaping here) from the registry. It doesn't matter on install because in that case I explicitly set it with an input file. This include is used before any components in my wxs file.</p>
  
  <pre><code>&lt;Include xmlns="http://schemas.microsoft.com/wix/2006/wi"&gt;
  
  &lt;?ifndef SetupXWinServiceRegistryProperties ?&gt;
  &lt;?define SetupXWinServiceRegistryProperties ?&gt;
  
  &lt;?define XWinServiceRegistryKey='Software\X\Y'?&gt;
  
  &lt;Property Id="X_WIN_SERVICE_NAME"&gt;
    &lt;RegistrySearch Id="XWinServiceNameSearch"
                      Root="HKLM"
                      Key="$(var.XWinServiceRegistryKey)"
                      Name="ServiceName"
                      Type="raw"/&gt;
  &lt;/Property&gt;
  
  &lt;?endif?&gt;
  &lt;/Include&gt;
  </code></pre>
  
  <p>The following include component is used to save the registry value on install:</p>
  
  <pre><code>&lt;Include xmlns="http://schemas.microsoft.com/wix/2006/wi"&gt;
  
  &lt;?ifndef WriteXWinServiceRegistryProperties ?&gt;
  &lt;?define WriteXWinServiceRegistryProperties ?&gt;
  
  &lt;Component Id="CompWriteXWinServiceRegistryProps"
    Guid="some guid"&gt;
  
  &lt;!-- Write properties to the registry. Then they will be 
         accessable during uninstall. --&gt;
  
  &lt;RegistryValue Root="HKLM"
     Key="$(var.XWinServiceRegistryKey)"
     Name="ServiceName"
     Type="string"
     Value="[X_WIN_SERVICE_NAME]"
     Action="write" /&gt;
  
  &lt;/Component&gt;
  
  &lt;?endif?&gt;
  
  &lt;/Include&gt;
  </code></pre>
  
  <p>I have checked my system after install and the registry value is properly written there. The part in my component where the service is setup looks like:</p>
  
  <pre><code>          &lt;ServiceInstall Id="ServiceInstallXWinService"
                            Name="[X_WIN_SERVICE_NAME]"
                            Start="auto"
                            DisplayName="xxx"
                            Description="yyy"
                            Account="[X_WIN_SERVICE_USER]"
                            Password="[X_WIN_SERVICE_PASSWORD]"
                            Type="ownProcess"
                            ErrorControl="normal"
                            Vital="yes" /&gt;
  
            &lt;ServiceControl Id="ServiceInstallXWinService" 
                            Name="[X_WIN_SERVICE_NAME]"
                            Stop="both"
                            Remove="uninstall"
                            Wait="yes" /&gt;
  </code></pre>
  
  <p>Any ideas?</p>