Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The Windows API supports objects called "Job Objects". The following code will create a "job" that is configured to shut down all processes when the main application ends (when its handles are cleaned up). This code should only be run once.:</p> <pre class="lang-cpp prettyprint-override"><code>HANDLE ghJob = CreateJobObject( NULL, NULL); // GLOBAL if( ghJob == NULL) { ::MessageBox( 0, "Could not create job object", "TEST", MB_OK); } else { JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli = { 0 }; // Configure all child processes associated with the job to terminate when the jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; if( 0 == SetInformationJobObject( ghJob, JobObjectExtendedLimitInformation, &amp;jeli, sizeof(jeli))) { ::MessageBox( 0, "Could not SetInformationJobObject", "TEST", MB_OK); } } </code></pre> <p>Then when each child process is created, execute the following code to launch each child each process and add it to the job object:</p> <pre class="lang-cpp prettyprint-override"><code>STARTUPINFO info={sizeof(info)}; PROCESS_INFORMATION processInfo; // Launch child process - example is notepad.exe if (::CreateProcess( NULL, "notepad.exe", NULL, NULL, TRUE, 0, NULL, NULL, &amp;info, &amp;processInfo)) { ::MessageBox( 0, "CreateProcess succeeded.", "TEST", MB_OK); if(ghJob) { if(0 == AssignProcessToJobObject( ghJob, processInfo.hProcess)) { ::MessageBox( 0, "Could not AssignProcessToObject", "TEST", MB_OK); } } // Can we free handles now? Not sure about this. //CloseHandle(processInfo.hProcess); CloseHandle(processInfo.hThread); } </code></pre> <p>VISTA NOTE: See <a href="http://social.msdn.microsoft.com/Forums/en-US/windowssecurity/thread/71c9599e-a3d5-4b79-bfc1-1800565c5b8a" rel="noreferrer">AssignProcessToJobObject always return "access denied" on Vista</a> if you encounter access-denied issues with AssignProcessToObject() on vista.</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