title: WiX: stop service before RemoveExistingProducts OR run elevated CustomAction to stop process before RemoveExistingProducts
body: <p>I ran into a problem on an major upgrade. The installer includes a service and on an upgrade I got a popup saying that a reboot is required to finish the setup process. </p>
  
  <p>To prevent this behaviour I actually just need to stop the service before <code>RemoveExistingProducts</code> (rather <code>InstallValidate</code>) is executed.</p>
  
  <p>The <code>MajorUpgrade</code> is placed after <code>InstallInitialize</code> and the package has <code>InstallPrivileges="elevated"</code>.</p>
  
  <p>I have two cases: </p>
  
  <hr>
  
  <p><strong>Case 1:</strong> The service is installed by <code>ServiceInstall</code> via</p>
  
  <pre><code>&lt;Component Id="myservice_Service" Guid="*"&gt;
              &lt;File Id="myservice.exe" KeyPath="yes" Vital="yes"
                    Source="SourceDir\bin\myservice.exe"/&gt;
              &lt;ServiceInstall Id="myservice_ServiceInstall" Type="ownProcess"
                              Vital="yes" Name="myservice" DisplayName="myservice Service"
                              Description="myservice Service" Start="auto" Account=".\LocalSystem"
                              ErrorControl="ignore" Interactive="no" Arguments="--run"/&gt;
              &lt;ServiceControl Id="myservice_ServiceControl" Name="myservice" Wait="yes" Stop="uninstall"/&gt;
          &lt;/Component&gt;
  </code></pre>
  
  <p>The <code>ServiceControl</code> is not stopping the service before <code>InstallValidate</code> is called. Even when saying Stop="both". So the popup appears. Note that the service is not started by the installer.</p>
  
  <p>Reasonable posts I've found (excerpt):</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/1485243/how-does-one-stop-a-windows-service-to-do-an-upgrade-install">How does one stop a Windows service to do an upgrade install?</a></li>
  <li><a href="https://stackoverflow.com/questions/327895/upgrade-a-windows-service-without-uninstalling">Upgrade a Windows Service without Uninstalling</a></li>
  </ul>
  
  <hr>
  
  <p><strong>Case 2:</strong> The service is installed by a <code>CustomAction</code> (there are some reasons why not doing via <code>ServiceInstall</code> as well).
  In this case I have to call an executable to stop the service ("myservice.exe --stop"). For this it's getting tricky, since due to ICE63 it's not allowed to schedule a <code>CustomAction</code> before <code>RemoveExistingProducts</code> is called. So, how may I achieve this anyway?</p>
  
  <p>So far I've read posts like:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/9836773/wix-call-app-on-uninstall-before-user-prompt-close-manually">WiX call app on uninstall before User prompt &quot;close manually&quot;</a></li>
  <li><a href="https://stackoverflow.com/questions/9822010/wix-close-application-before-uninstall-close-open-applications-message">WiX close application before uninstall - close open applications message</a></li>
  <li><a href="https://stackoverflow.com/questions/26281648/close-a-systemtray-app-before-uninstalling-using-wix">Close a systemtray app before uninstalling using wix</a></li>
  <li><a href="http://t53456.windows-development-wix-user.wintalk.us/stop-a-service-before-uninstall-t53456.html" rel="noreferrer">http://t53456.windows-development-wix-user.wintalk.us/stop-a-service-before-uninstall-t53456.html</a></li>
  </ul>
  
  <p>A bootstrapper exe is no option, since I need to produce an plain MSI.</p>
  
  <p>I've found similar unanswered problem here: <a href="https://stackoverflow.com/questions/6913332/wix-installer-problem-why-does-restartmanager-mark-service-as-rmcritical-and-no">Wix Installer Problem: Why does RestartManager mark Service as RMCritical and not RMService</a></p>