diff --git a/.cspell/words.txt b/.cspell/words.txt index 40be5f7a..aac39b51 100644 --- a/.cspell/words.txt +++ b/.cspell/words.txt @@ -207,6 +207,7 @@ source_subdir spdlog spdlog_project st_ctim +startupinfoa static-libgcc static-libstdc++ stbuf diff --git a/repertory/repertory/src/ui/handlers.cpp b/repertory/repertory/src/ui/handlers.cpp index 97b6e8f6..c8e5cd75 100644 --- a/repertory/repertory/src/ui/handlers.cpp +++ b/repertory/repertory/src/ui/handlers.cpp @@ -38,6 +38,7 @@ #include #include #include +namespace bp1 = boost::process::v1; namespace { [[nodiscard]] auto decrypt(std::string_view data, std::string_view password) @@ -720,19 +721,35 @@ auto handlers::launch_process(provider_type prov, std::string_view name, recur_mutex_lock inst_lock(inst_mtx); if (background) { #if defined(_WIN32) - std::array path{}; - ::GetSystemDirectoryA(path.data(), path.size()); + args.insert(args.begin(), "--hidden"); - args.insert(args.begin(), utils::path::combine(path.data(), {"cmd.exe"})); - args.insert(std::next(args.begin()), "/c"); - args.insert(std::next(args.begin(), 2U), "start"); - args.insert(std::next(args.begin(), 3U), ""); - args.insert(std::next(args.begin(), 4U), "/MIN"); - args.insert(std::next(args.begin(), 5U), repertory_binary_); + auto cmdline = fmt::format("{}{}{}", '"', repertory_binary_, '"'); + for (const auto &arg : args) { + cmdline += " "; + if (arg.find_first_of(" \t") != std::string::npos) { + cmdline += fmt::format("{}{}{}", '"', arg, '"'); + } else { + cmdline += arg; + } + } + + STARTUPINFOA start_info{}; + start_info.cb = sizeof(start_info); + + PROCESS_INFORMATION proc_info{}; + auto result = ::CreateProcessA( + nullptr, &cmdline[0U], nullptr, nullptr, FALSE, + CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS, nullptr, + utils::path::get_parent_path(repertory_binary_).c_str(), &start_info, + &proc_info); + + if (result) { + ::CloseHandle(proc_info.hProcess); + ::CloseHandle(proc_info.hThread); + } #else // !defined(_WIN32) - args.insert(args.begin(), "-f"); args.insert(args.begin(), repertory_binary_); -#endif // defined(_WIN32) + args.insert(std::next(args.begin()), "-f"); std::vector exec_args; exec_args.reserve(args.size() + 1U); @@ -741,10 +758,6 @@ auto handlers::launch_process(provider_type prov, std::string_view name, } exec_args.push_back(nullptr); -#if defined(_WIN32) - _spawnv(_P_DETACH, exec_args.at(0U), - const_cast(exec_args.data())); -#else // !defined(_WIN32) auto pid = fork(); if (pid < 0) { exit(1); @@ -778,10 +791,8 @@ auto handlers::launch_process(provider_type prov, std::string_view name, return {}; } - boost::process::v1::ipstream out; - boost::process::v1::child proc(repertory_binary_, - boost::process::v1::args(args), - boost::process::v1::std_out > out); + bp1::ipstream out; + bp1::child proc(repertory_binary_, bp1::args(args), bp1::std_out > out); std::string data; std::string line;