The DebugSetProcessKillOnExit
function sets or retrieves the value that indicates whether a process is to be terminated when it exits.
This function is part of the Windows Debugging API.
BOOL DebugSetProcessKillOnExit(
HANDLE processHandle,
BOOL killOnExit
);
A handle to the process whose exit behavior is to be controlled.
If this parameter is TRUE
, the process will be terminated when it exits. If FALSE
, the process will not be terminated when it exits.
The function returns TRUE
if the call is successful, and FALSE
if it fails. Errors can be detected by calling DebugGetProcessKillOnExit
.
The following C code demonstrates how to set the killOnExit
flag to TRUE
for a specific process:
#include
#include
int main() {
HANDLE hProcess;
DWORD dwExitCode;
// Open the process
hProcess = CreateProcess("notepad.exe", NULL, NULL, NULL, FALSE,
CREATE_NO_WINDOW, NULL, NULL, NULL);
if (hProcess != NULL) {
// Set kill on exit flag
if (DebugSetProcessKillOnExit(hProcess, TRUE)) {
printf("Kill on exit set for process.\n");
Sleep(5000); // Wait 5 seconds
TerminateProcess(hProcess, 0);
} else {
printf("Failed to set kill on exit.\n");
}
} else {
printf("Failed to create process.\n");
}
CloseHandle(hProcess);
return 0;
}