title: OpenProcess: Is it possible to get ERROR_ACCESS_DENIED for PROCESS_QUERY_LIMITED_INFORMATION but not for SYNCHRONIZE?
body: <p>I'm using <code>OpenProcess</code> to get the process handle from a PID.
  The two tasks the function should do is:</p>
  
  <ul>
  <li>must have: wait for the process termination, done with <code>WaitForSingleObject (process, INFINITE)</code></li>
  <li>if possible: get exit code, done with <code>GetExitCodeProcess (process, &amp;ret)</code></li>
  </ul>
  
  <p><strong>Question</strong>: Is it possible to get <code>ERROR_ACCESS_DENIED</code> for <code>PROCESS_QUERY_LIMITED_INFORMATION</code> but not for <code>SYNCHRONIZE</code>? If yes: which scenario?</p>
  
  <p>My full code for reference:</p>
  
  <pre><code>/* wait for a pid to end and return its exit code
     error codes are returned as negative value
  */
  int
  waitpid (const int pid)
  {
      int status = 0;
      HANDLE process = NULL;
      DWORD ret;
  
      /* windows will wait for the own process to end... abort */
      if (pid == _getpid ()) {
          status = 0 - ERROR_INVALID_DATA;
          return status;
      }
      /* get process handle */
      process = OpenProcess (SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
      /* if we don't get access to query the process' exit status try to get at least
          access to the process end (needed for WaitForSingleObject)
      */
      if (!process &amp;&amp; GetLastError () == ERROR_ACCESS_DENIED) {
          OpenProcess (SYNCHRONIZE, FALSE, pid);
          status = -2;
      }
      if (process) {
          /* wait until process exit */
          ret = WaitForSingleObject (process, INFINITE);
          if (ret == WAIT_FAILED) {
              status = 0 - GetLastError ();
          /* get exit code, if possible */
          } else if (status != -2) { 
              if (!GetExitCodeProcess (process, &amp;ret)) {
                  status = 0 - GetLastError ();
              } else {
                  status = (int) ret;
              }
          }
          CloseHandle (process);
      } else {
          status = 0 - GetLastError ();
      }
      return status;
  }
  </code></pre>
  
  <p>(if you have any comments to the code: use the comments and share your thoughts)</p>