title: OpenProcess failed to open existing process via id (Last Error code: 1008)?
body: <p>I'm trying to use <code>OpenProcess</code> to open all existing processes via the process ID. But somehow it works just the first call, while the next calls show that it does not work with an error code reported as <code>1008</code> (<em>An attempt was made to reference a token that does not exist</em>). </p>
  
  <p>Here is the code:</p>
  
  <pre><code>[DllImport("kernel32.dll", SetLastError=true)]
  public static extern IntPtr OpenProcess(ProcessAccessFlags access, bool inheritHandle, int procId);
  [DllImport("kernel32.dll", SetLastError = true)]
  [return: MarshalAs(UnmanagedType.Bool)]
  static extern bool CloseHandle(IntPtr hObject);
  
  [Flags]
  public enum ProcessAccessFlags : uint
  {
      All = 0x001F0FFF,
      Terminate = 0x00000001,
      CreateThread = 0x00000002,
      VirtualMemoryOperation = 0x00000008,
      VirtualMemoryRead = 0x00000010,
      VirtualMemoryWrite = 0x00000020,
      DuplicateHandle = 0x00000040,
      CreateProcess = 0x000000080,
      SetQuota = 0x00000100,
      SetInformation = 0x00000200,
      QueryInformation = 0x00000400,
      QueryLimitedInformation = 0x00001000,
      Synchronize = 0x00100000
  }
  
  foreach (var proc in Process.GetProcesses()) {
      var procHandle = OpenProcess(ProcessAccessFlags.QueryInformation | ProcessAccessFlags.VirtualMemoryOperation, false, proc.Id);
      var eCode = Marshal.GetLastWin32Error(); 
      //checking the last error code using Marshal.GetLastWin32Error() 
      //shows that it succeeds the first time with error code of 0
      //Then all the next calls show error code of `1008`.
      if(procHandle != IntPtr.Zero) CloseHandle(procHandle);
  }
  </code></pre>
  
  <p>I've tried googling for the error but not sure what could be wrong here. It would be nice if you can let me know what's wrong. Thank you!</p>
  
  <p><strong>UPDATE:</strong>
  As I said it seems to work just for the first process in the loop. BUT I highly doubt that it actually does not work even for that case because from what I see the <code>procHandle</code> keeping a totally different value from the <code>proc.Handle</code>, <strong><em>unless</em></strong> the handle returned by <code>OpenProcess</code> is <em>another kind</em> of handle and not the same to <code>proc.Handle</code> (if so it's really weird). So if what I'm currently doubting is true, that means <code>OpenProcess</code> totally does not work at all. It's simply useless in this case and still not sure in which context we can use it.</p>