Преглед на файлове

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

takase1121 преди 3 месеца
родител
ревизия
95c44dcdc3
променени са 1 файла, в които са добавени 15 реда и са изтрити 7 реда
  1. 15 7
      src/process/windows/SDL_windowsprocess.c

+ 15 - 7
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, &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");