Skip to content

Process(windows): try sending WM_CLOSE for graceful process termination #13108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/process/windows/SDL_windowsprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,31 @@ bool SDL_SYS_CreateProcessWithProperties(SDL_Process *process, SDL_PropertiesID
return result;
}

static BOOL CALLBACK terminate_app(HWND hwnd, LPARAM lparam)
{
DWORD current_proc_id = 0, *term_info = (DWORD *) lparam;
GetWindowThreadProcessId(hwnd, &current_proc_id);
if (current_proc_id == term_info[0] && PostMessage(hwnd, WM_CLOSE, 0, 0)) {
term_info[1]++;
}
return TRUE;
}

bool SDL_SYS_KillProcess(SDL_Process *process, bool force)
{
if (!force) {
// 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");
}
Expand Down
Loading