title: How can I reject a Windows "Service Stop" request in ATL 7?
body: <p>I have a Windows service built upon ATL 7's <code>CAtlServiceModuleT</code> class.  This service serves up COM objects that are used by various applications on the system, and these other applications naturally start getting errors if the service is stopped while they are still running.</p>
  <p>I know that ATL DLLs solve this problem by returning S_OK in <code>DllCanUnloadNow()</code> if CComModule's <code>GetLockCount()</code> returns 0.  That is, it checks to make sure no one is currently using any COM objects served up by the DLL.  I want equivalent functionality in the service.</p>
  <p>Here is what I've done in my override of <code>CAtlServiceModuleT::OnStop()</code>:</p>
  <pre><code>void CMyServiceModule::OnStop()
  {
      if( GetLockCount() != 0 ) {
          return;
      }
  
      BaseClass::OnStop();
  }
  </code></pre>
  <p>Now, when the user attempts to Stop the service from the Services panel, they are presented with an error message:</p>
  <blockquote>
  <p>Windows could not stop the XYZ service on Local Computer.
  The service did not return an error.  This could be an internal Windows error or an internal service error.
  If the problem persists, contact your system administrator.</p>
  </blockquote>
  <p>The Stop request is indeed refused, but it appears to put the service in a bad state.  A second Stop request results in this error message:</p>
  <blockquote>
  <p>Windows could not stop the XYZ service on Local Computer.</p>
  <p>Error 1061: The service cannot accept control messages at this time.</p>
  </blockquote>
  <p>Interestingly, the service does actually stop this time (although I'd rather it not, since there are still outstanding COM references).</p>
  <p>I have two questions:</p>
  <ol>
  <li>Is it considered bad practice for a service to refuse to stop when asked?</li>
  <li>Is there a polite way to signify that the Stop request is being refused; one that doesn't put the Service into a bad state?</li>
  </ol>