improve console check in gspawn-win32

We use the return value of AllocConsole (GetCurrentProcessId ()) to
determine whether the spawning process is attached to a console.
This commit is contained in:
Princeton Ferro 2021-04-19 10:25:06 +00:00 committed by Philip Withnall
parent d602523988
commit 30ed2ea263

View File

@ -530,6 +530,28 @@ do_spawn_directly (gint *exit_status,
return TRUE; return TRUE;
} }
static gboolean
might_be_console_process (void)
{
// we should always fail to attach ourself to a console (because we're
// either already attached, or we do not have a console)
gboolean attached_to_self = AttachConsole (GetCurrentProcessId ());
g_return_val_if_fail (!attached_to_self, TRUE);
switch (GetLastError ())
{
// current process is already attached to a console
case ERROR_ACCESS_DENIED:
return TRUE;
// current process does not have a console
case ERROR_INVALID_HANDLE:
return FALSE;
// we should not get ERROR_INVALID_PARAMETER
}
g_return_val_if_reached (FALSE);
}
static gboolean static gboolean
fork_exec (gint *exit_status, fork_exec (gint *exit_status,
gboolean do_return_handle, gboolean do_return_handle,
@ -625,7 +647,7 @@ fork_exec (gint *exit_status,
goto cleanup_and_fail; goto cleanup_and_fail;
new_argv = g_new (char *, argc + 1 + ARG_COUNT); new_argv = g_new (char *, argc + 1 + ARG_COUNT);
if (GetConsoleWindow () != NULL) if (might_be_console_process ())
helper_process = HELPER_PROCESS "-console.exe"; helper_process = HELPER_PROCESS "-console.exe";
else else
helper_process = HELPER_PROCESS ".exe"; helper_process = HELPER_PROCESS ".exe";