From fdb859433922ff59107e28e187a1b81ff3118c38 Mon Sep 17 00:00:00 2001 From: takase1121 <20792268+takase1121@users.noreply.github.com> Date: Sat, 24 May 2025 14:35:38 +0800 Subject: [PATCH] process(windows): fallback to GenerateConsoleCtrlEvent and TerminateProcess if necessary --- src/process/windows/SDL_windowsprocess.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/process/windows/SDL_windowsprocess.c b/src/process/windows/SDL_windowsprocess.c index 3ba98af97c..cae6cfe1fc 100644 --- a/src/process/windows/SDL_windowsprocess.c +++ b/src/process/windows/SDL_windowsprocess.c @@ -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, ¤t_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");