Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes, it is possible. However, observe:</p> <pre><code>// This example will fail to reopen and terminate the job. // Processes cmd and ping stay running STARTUPINFO si; memset(&amp;si, 0, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); PROCESS_INFORMATION pi; memset(&amp;pi, 0, sizeof(PROCESS_INFORMATION)); // BEGIN BLOCK A ::CreateProcess(NULL, "cmd /c ping 127.0.0.1 -n 10 &gt; nul", NULL, NULL, TRUE, CREATE_SUSPENDED, NULL, ".", &amp;si, &amp;pi); HANDLE job = ::CeateJobObject(NULL, "myjob"); ::AssignProcessToJobObject(job, pi.hProcess); // END BLOCK A ::ResumeThread(pi.hThread); ::CloseHandle(pi.hProcess); ::CloseHandle(pi.hThread); ::CloseHandle(job); HANDLE reopenJob = ::OpenJobObject(JOB_OBJECT_TERMINATE, FALSE, "myjob"); if(reopenJob != NULL) ::TerminateJobObject(reopenJob, 1); </code></pre> <p>The reopened job in this case is NULL, and the error code is ERROR_FILE_NOT_FOUND.</p> <p>The job must be created before the process, and be inherited, for the job to be reopenable.</p> <pre><code>// NEW BLOCK A HANDLE job = ::CreateJobObject(NULL, "myjob"); ::SetHandleInformation(job, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT); ::CreateProcess(NULL, "cmd /c ping 127.0.0.1 -n 10 &gt; nul", NULL, NULL, TRUE, CREATE_SUSPENDED, NULL, ".", &amp;si, &amp;pi); ::AssignProcessToJobObject(job, pi.hProcess); </code></pre> <p>If your goal is to name the job using the PID, note that this creation order prevents such a convention. This still means that some piece of information must be tracked in addition to the PID in order to reopen the job.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload