CreateJobObject behaviour when already inside a job
<p>My scenario: I have a C program in Windows that, in running time, corresponds to  <code>process A</code>; it needs to start another process (<code>process B</code>) and monitor it so that, under some external event (say, lock file removed) it can terminate <code>B</code> and all its eventual children (processes started by <code>B</code>). </p>

<p>My approach was to place <code>B</code> into a job created with <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms682409(v=vs.85).aspx" rel="nofollow noreferrer">CreateJobObject</a>, so that process <code>A</code> can terminate it (together with its children) with <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms686709(v=vs.85).aspx" rel="nofollow noreferrer">TerminateJobObject</a> - and then it can terminate itself.</p>

<pre><code>HANDLE jobHandle = CreateJobObject(NULL, NULL); // creates job
...
res=CreateProcess(NULL,cmdline, .... &amp;pi);  // creates process B
AssignProcessToJobObject(jobHandle,pi.hProcess); // add process B to job
...
if(...)  { 
    TerminateJobObject(jobHandle,exitCode);  // terminate job: process B and children
    ....
}
</code></pre>

<p>This works. Except that, under certain circumstances [*] the process <code>A</code> happens to be already included in a job. In this case <code>CreateJobObject(NULL, NULL)</code> does not create a new job, but it returns the current one - which is not what I want.</p>

<p>How do create a wholly new job?</p>

<p>I don't want to rely on nested jobs, because I want to support Windows 7.</p>

<p>[*] I'm looking at you, Eclipse - but that does not matter much now.</p>