From 35072da2ea52a5070b89507a8b0c436d3f820349 Mon Sep 17 00:00:00 2001 From: ergo720 <45463469+ergo720@users.noreply.github.com> Date: Sun, 3 Oct 2021 10:15:48 +0200 Subject: [PATCH 1/2] Fixed invalid handle exception in freopen --- src/common/win32/InlineFunc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/win32/InlineFunc.cpp b/src/common/win32/InlineFunc.cpp index 5b0894abe..9e255c084 100644 --- a/src/common/win32/InlineFunc.cpp +++ b/src/common/win32/InlineFunc.cpp @@ -69,7 +69,7 @@ std::optional CxbxExec(bool useDebugger, HANDLE* hProcess, bool req Plus ShellExecute is high level whilst CreateProcess is low level. We want to use official low level functions as possible to reduce cpu load cycles to get the task done. */ - if (CreateProcess(nullptr, const_cast(szProcArgsBuffer.c_str()), nullptr, nullptr, false, 0, nullptr, nullptr, &startupInfo, &processInfo) == 0) { + if (CreateProcess(nullptr, const_cast(szProcArgsBuffer.c_str()), nullptr, nullptr, false, DETACHED_PROCESS, nullptr, nullptr, &startupInfo, &processInfo) == 0) { return std::make_optional("Failed to create the new emulation process. CreateProcess failed because: " + WinError2Str()); } CloseHandle(processInfo.hThread); From b52f5655e41641799bfd3d2248ed6f73db6d5d6b Mon Sep 17 00:00:00 2001 From: ergo720 <45463469+ergo720@users.noreply.github.com> Date: Sun, 3 Oct 2021 12:08:47 +0200 Subject: [PATCH 2/2] Added comment explaining the DETACHED_PROCESS flag --- src/common/win32/InlineFunc.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/common/win32/InlineFunc.cpp b/src/common/win32/InlineFunc.cpp index 9e255c084..ce0d2701e 100644 --- a/src/common/win32/InlineFunc.cpp +++ b/src/common/win32/InlineFunc.cpp @@ -68,6 +68,9 @@ std::optional CxbxExec(bool useDebugger, HANDLE* hProcess, bool req Using ShellExecute has proper implement. Unfortunately, we need created process handle for Debugger monitor. Plus ShellExecute is high level whilst CreateProcess is low level. We want to use official low level functions as possible to reduce cpu load cycles to get the task done. + + Without the DETACHED_PROCESS flag, the default behavior would be for the new process to inherit the console of the parent process, + which is wrong since we want the emulation process to have its own console allocated with AllocConsole instead. */ if (CreateProcess(nullptr, const_cast(szProcArgsBuffer.c_str()), nullptr, nullptr, false, DETACHED_PROCESS, nullptr, nullptr, &startupInfo, &processInfo) == 0) { return std::make_optional("Failed to create the new emulation process. CreateProcess failed because: " + WinError2Str());