If I make the Win32 API calls below:
How can I set PROCESS_TERMINATE and SYNCHRONIZE for lpProcessAttributes and have them inherit to child processes so I can terminate all child processes?
PROCESS_INFORMATION process_info;
STARTUPINFO startup_info;
ZeroMemory(&process_info, sizeof(PROCESS_INFORMATION));
ZeroMemory(&startup_info, sizeof(STARTUPINFO));
SetLastError(0);
BOOL bSuccess = CreateProcess(
/*[in, optional] LPCSTR lpApplicationName */ nullptr,
/*[in, out, optional] LPSTR lpCommandLine */
"powershell -command dir C:\\windows -recurse",
/*[in, optional] LPSECURITY_ATTRIBUTES lpProcessAttributes */ nullptr,
/*[in, optional] LPSECURITY_ATTRIBUTES lpThreadAttributes */ nullptr,
/*[in] BOOL bInheritHandles */ TRUE,
/*[in] DWORD dwCreationFlags */ 0,
/*[in, optional] LPVOID lpEnvironment */ nullptr,
/*[in, optional] LPCSTR lpCurrentDirectory */ "C:\\",
/*[in] LPSTARTUPINFOA lpStartupInfo */ &startup_info,
/*[out] LPPROCESS_INFORMATION lpProcessInformation */ &process_info
);
TerminateProcess(process_info.hProcess, 0);
// 500 ms timeout; use INFINITE for no timeout
const DWORD result = WaitForSingleObject(
process_info.hProcess, 500);
if (result == WAIT_OBJECT_0) {
// Success
}
else {
// Timed out or an error occurred
}
CloseHandle(process_info.hProcess);
CloseHandle(process_info.hThread);
CodePudding user response:
According to the Doc:Process Security and Access Rights
The handle returned by the CreateProcess function has PROCESS_ALL_ACCESS access to the process object.
According to the Doc:TerminateProcess function
A handle to the process to be terminated.
The handle must have the PROCESS_TERMINATE access right.
As far as I'm concerned, the handle return by CreateProcess should have necessary and sufficient access-rights to allow killing the process using TerminateProcess.
terminate all child processes
If you wang to terminate all child processes,I suggest you could try to use JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
.
For more details, I suggest you could refer to the Bilog:Destroying all child processes (and grandchildren) when the parent exits
CodePudding user response:
void KillDash9_v2(unsigned long PID) {
// Clear Windows Error Variable
SetLastError(0);
HANDLE pHandle = OpenProcess(
/*[in] DWORD dwDesiredAccess*/ PROCESS_TERMINATE | SYNCHRONIZE,
/*[in] BOOL bInheritHandle*/ FALSE,
/*[in] DWORD dwProcessId*/ PID
);
if (pHandle == nullptr) {
long int err = GetLastError();
std::string msg = std::string("ERROR : OpenProcess failed for PID(") std::to_string(PID) std::string(")\n")
std::string("REASON : ") WindowsGetErrorString(err) std::string("\n")
std::string("CODE : ") std::to_string(err) std::string("\n");
std::cout << msg;
return;
}
// Clear Windows Error Variable
SetLastError(0);
BOOL rc = TerminateProcess(
pHandle,
-1 // Process Return code triggered by TerminateProcess
);
if (rc == 0) {
long int err = GetLastError();
std::string msg = std::string("ERROR: TerminateProcess failed for PID(") std::to_string(PID) std::string(")\n")
std::string("REASON: ") WindowsGetErrorString(err) std::string("\n")
std::string("CODE : ") std::to_string(err) std::string("\n");
std::cout << msg;
return;
}
WaitForSingleObject(pHandle, 500);
CloseHandle(pHandle);
}