title: Cleanest way to stop a process on Win32?
body: <p>While implementing an applicative server and its client-side libraries in C++, I am having trouble finding a clean and reliable way to stop client processes on server shutdown on Windows.</p>
  
  <p>Assuming the server and its clients run under the same user, the requirements are:</p>
  
  <ul>
  <li>the solution should work in the following cases:
    <ul>
    <li>clients may each feature either a console or a gui.</li>
    <li>user may be unprivileged.<li>
    <li>clients may be or become unresponsive (infinite loop, deadlock).</li>
    <li>clients may or may not be children of the server (direct or indirect).</li>
    </ul>
  <li>unless prevented by a client-side defect, clients shall be allowed the opportunity to exit cleanly (free their ressources, sync some data to disk...) and some reasonable time to do so.</li>
  <li>all client return codes shall be made available (if possible) to the server during the shutdown procedure.</li>
  <li>server shall wait until all clients are gone.</li>
  </ul>
  
  <hr>
  
  <p>As of this edit, the majority of the answers below advocate the use of a shared memory (or another IPC mechanism) between the server and its clients to convey shutdown orders and client status. These solutions would work, but require that clients successfully initialize the library.</p>
  
  <p>What I did not say, is that the server is also used to start the clients and in some cases other programs/scripts which don't use the client library at all. A solution that did not rely on a graceful communication between server and clients would be nicer (if possible).</p>
  
  <p>Some time ago, I stumbled upon a C snippet (in the MSDN I believe) that did the following:</p>
  
  <ol>
  <li>start a thread via CreateRemoteThread in the process to shutdown.</li>
  <li>had that thread directly call ExitProcess.</li>
  </ol>
  
  <p>Unfortunately now that I'm looking for it, I'm unable to find it and the search results seem to imply that this trick does not work anymore on Vista. Any expert input on this ?</p>