From 7e03a071defda42a7049bca9793defb218a1f4d6 Mon Sep 17 00:00:00 2001 From: IG Date: Thu, 1 Feb 2024 10:48:44 +0000 Subject: [PATCH] wait for the process to start before closing handles --- .github/workflows/build.yml | 2 +- bt/app/browser.cpp | 30 +++++++++++++++++++++++++++++- bt/app/browser.h | 1 + docs/release-notes.md | 6 ++++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 309582d..2c7972e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,7 +1,7 @@ name: 'Build' env: - VERSION: 3.7.0 + VERSION: 3.7.1 BUILD_TYPE: Release ARCH: x64 VCPKG_CONFIG: Release diff --git a/bt/app/browser.cpp b/bt/app/browser.cpp index cdc754c..addaad2 100644 --- a/bt/app/browser.cpp +++ b/bt/app/browser.cpp @@ -228,7 +228,8 @@ namespace bt { } //win32::shell::exec(b->open_cmd, arg); - win32::process::start(b->open_cmd + " " + arg, false); + //win32::process::start(b->open_cmd + " " + arg, false); + launch_win32_process_and_foreground(b->open_cmd + " " + arg); } bool browser_instance::is_match(const url_payload& up, match_rule& mr) const { @@ -290,4 +291,31 @@ namespace bt { } } } + + void browser_instance::launch_win32_process_and_foreground(const std::string& cmdline) const { + STARTUPINFO si{}; + PROCESS_INFORMATION pi{}; + DWORD pid{0}; + + if(::CreateProcess(nullptr, + const_cast(str::to_wstr(cmdline).c_str()), + nullptr, + nullptr, + false, + 0, + nullptr, + nullptr, + &si, + &pi)) { + + // Wait for the process to start before closing the handles, + // otherwise the process will be terminated (browser will be shown in the background). + + // Wait for 5 seconds maximum + ::WaitForSingleObject(pi.hProcess, 5000); + + ::CloseHandle(pi.hProcess); + ::CloseHandle(pi.hThread); + } + } } \ No newline at end of file diff --git a/bt/app/browser.h b/bt/app/browser.h index effdf82..189fbbe 100644 --- a/bt/app/browser.h +++ b/bt/app/browser.h @@ -152,6 +152,7 @@ namespace bt { void set_rules_from_text(std::vector rules_txt); private: + void launch_win32_process_and_foreground(const std::string& cmdline) const; }; struct browser_match_result { diff --git a/docs/release-notes.md b/docs/release-notes.md index 4b6bded..4703606 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -1,3 +1,9 @@ +## 3.7.1 + +### Improvements + +- Hopefully fixed browser being backgrounded for some users (#39). + ## 3.7.0 ### New features