From a0a049a9206db32e70b72885a10f1e5273411a2d Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 7 Jan 2026 17:12:58 +0100 Subject: [PATCH] fix: ImHex not always exiting when forwarder is killed --- main/forwarder/source/main.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/main/forwarder/source/main.cpp b/main/forwarder/source/main.cpp index 823807cc0..472967c0d 100644 --- a/main/forwarder/source/main.cpp +++ b/main/forwarder/source/main.cpp @@ -94,6 +94,12 @@ int launchExecutable() { // Ensure the read handle to stdout is not inherited ::SetHandleInformation(hChildStdoutRead, HANDLE_FLAG_INHERIT, 0); + // Create a job object to ensure the child process is terminated when this process exits + auto hJob = ::CreateJobObjectW(nullptr, nullptr); + JOBOBJECT_EXTENDED_LIMIT_INFORMATION info {}; + info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; + ::SetInformationJobObject(hJob, JobObjectExtendedLimitInformation, &info, sizeof(info)); + // Set up the STARTUPINFO structure for the child process STARTUPINFOW si; ::ZeroMemory(&si, sizeof(STARTUPINFOW)); @@ -113,7 +119,7 @@ int launchExecutable() { nullptr, // Process security attributes nullptr, // Thread security attributes TRUE, // Inherit handles - 0, // Creation flags + CREATE_SUSPENDED, // Creation flags nullptr, // Environment nullptr, // Current directory &si, // STARTUPINFO @@ -126,6 +132,20 @@ int launchExecutable() { return EXIT_FAILURE; } + if (::AssignProcessToJobObject(hJob, pi.hProcess)) { + if (::ResumeThread(pi.hThread) == -1) { + ::TerminateProcess(pi.hProcess, EXIT_FAILURE); + ::CloseHandle(hChildStdinRead); + ::CloseHandle(hChildStdinWrite); + ::CloseHandle(hChildStdoutRead); + ::CloseHandle(hChildStdoutWrite); + ::CloseHandle(pi.hProcess); + ::CloseHandle(pi.hThread); + return EXIT_FAILURE; + } + } + + // Close handles that the child inherited ::CloseHandle(hChildStdinRead); ::CloseHandle(hChildStdoutWrite);