process(windows): fallback to GenerateConsoleCtrlEvent and TerminateProcess if necessary

This commit is contained in:
takase1121
2025-05-24 14:35:38 +08:00
committed by Sam Lantinga
parent c709b8ed98
commit 95c44dcdc3

View File

@@ -520,12 +520,12 @@ done:
return result;
}
static BOOL CALLBACK terminate_app(HWND hwnd, LPARAM proc_id)
static BOOL CALLBACK terminate_app(HWND hwnd, LPARAM lparam)
{
DWORD current_proc_id = 0;
DWORD current_proc_id = 0, *term_info = (DWORD *) lparam;
GetWindowThreadProcessId(hwnd, &current_proc_id);
if (current_proc_id == (DWORD) proc_id) {
PostMessage(hwnd, WM_CLOSE, 0, 0);
if (current_proc_id == term_info[0] && PostMessage(hwnd, WM_CLOSE, 0, 0)) {
term_info[1]++;
}
return TRUE;
}
@@ -533,9 +533,17 @@ static BOOL CALLBACK terminate_app(HWND hwnd, LPARAM proc_id)
bool SDL_SYS_KillProcess(SDL_Process *process, bool force)
{
if (!force) {
EnumWindows(terminate_app, (LPARAM) process->internal->process_information.dwProcessId);
PostThreadMessage(process->internal->process_information.dwThreadId, WM_CLOSE, 0, 0);
return true;
// term_info[0] is the process ID, term_info[1] is number of successful tries
DWORD term_info[2];
term_info[0] = process->internal->process_information.dwProcessId;
term_info[1] = 0;
EnumWindows(terminate_app, (LPARAM) &term_info);
if (term_info[1] || PostThreadMessage(process->internal->process_information.dwThreadId, WM_CLOSE, 0, 0)) {
return true;
}
if (GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, term_info[0])) {
return true;
}
}
if (!TerminateProcess(process->internal->process_information.hProcess, 1)) {
return WIN_SetError("TerminateProcess failed");