title: Suspend/Resume a thread or process from another Thread in different process
body: <p>In one of my projects I create multiple processes (which are sub projects that I developed) from my main project as <strong>CREATE_SUSPENDED</strong> and storing their handles in a global array but; when I want to resume it with <em>ResumeThread</em> function I get <strong>ERROR_INVALID_HANDLE</strong> error code. </p>
  
  <p><a href="https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-resumethread" rel="nofollow noreferrer">The MSDN</a> specifies that the thread must have THREAD_SUSPEND_RESUME access right and I couldn't find how to set it.</p>
  
  <p>Do those handles are only valid on its process space and how can I suspend/resume a thread or process from another process with all the handles I stored? </p>
  
  <p>Note: I know synchronization objects may have a use here but is it the only and only way?  </p>
  
  <p>One more issue is; how to obtain handle of a thread created by my ProcessClass from the main process, which also created that ProcessClass?</p>
  
  <p>Here is a portion of the code</p>
  
  <p>ProcessClass</p>
  
  <pre><code>void ProcessClass::Start(){       
      STARTUPINFO si;
      PROCESS_INFORMATION pi;
      ZeroMemory(&amp;pi, sizeof(pi));
      ZeroMemory(&amp;si, sizeof(si));
      si.cb = sizeof(si);
  
      BOOL createdProc = CreateProcess(NULL, 
          ProcessExeDir, 
          NULL, 
          NULL, 
          TRUE, 
          CREATE_NEW_CONSOLE | CREATE_SUSPENDED
          , NULL, 
          NULL, 
          &amp;si, 
          &amp;pi); 
      if (createdProc == FALSE)
      {
          UINT32 errorCode = GetLastError();        
      }
      else
      {
          DWORD affinity;
          affinity = 0x000000010 &lt;&lt; (5);
          SetProcessAffinityMask(pi.hProcess, affinity);
  
          ProcessHandler = pi.hProcess;
          ProcessInfoPtr-&gt;ProcHandle = ProcessHandler;
  
          // I can need this handle
          // CloseHandle(pi.hThread);
      }
  }
  
  // ProcessInPtr is defines as ProcessInfoStc* in the header
  ProcessInfoStc* ProcessClass::GetProcessInfoPtr() {
      return ProcessInfoPtr;
  }
  </code></pre>
  
  <p>In the Main Project:</p>
  
  <pre><code>int main() {
     // this is declared as ManageProcesses(const ProcessInfoStcPtr&amp; processes)
      ManageProcesses(ProcessInfoArray);
      return 0;
  }
  </code></pre>
  
  <p>In ManageProcess function which is in the same lib project with the ProcessClass and following structure definitions: </p>
  
  <pre><code>ProcessClass processObj = new ProcessClass();
  processObj-&gt;Start();
  ...
  HANDLE pHandle = processObj-&gt;GetProcessInfoPtr()-&gt;ProcHandle;
  //! pHandle is is same with the created process handle as I debug
  ResumeThread(pHandle); 
  DWORD error = GetLastError(); // RETURNS INVALID HANDLE
  </code></pre>
  
  <p>And the Structure to store process is also in main process </p>
  
  <pre><code>ProcessInfoStc {
      HANDLE ProcHandle ,
      DWORD ProcId..
   }
   ProcessInfoStc ProcessInfoArray[10] = {
       {
          0,0...
       },...
   }
   typedef ProcessInfoStc* ProcessInfoStcPtr;
  </code></pre>