diff --git a/apps/desktop/src/app/main.cpp b/apps/desktop/src/app/main.cpp index 59bb55bf..14fb3791 100644 --- a/apps/desktop/src/app/main.cpp +++ b/apps/desktop/src/app/main.cpp @@ -17,6 +17,7 @@ #include #include "platform/windows/autostart_task.h" +#include "platform/windows/executable_path.h" #include "platform/windows/gui/slint_backend.h" #include "platform/windows/runtime_daemon.h" #include "platform/windows/service/autostart_control.h" @@ -95,23 +96,8 @@ void PrintServiceCliUsage() { "credentials\n"; } -std::wstring GetCurrentExecutablePathW() { - wchar_t path[MAX_PATH] = {0}; - DWORD length = GetModuleFileNameW(nullptr, path, MAX_PATH); - if (length == 0 || length >= MAX_PATH) { - return L""; - } - return std::wstring(path, length); -} - std::filesystem::path GetSiblingServiceExecutablePath() { - std::wstring current_executable = GetCurrentExecutablePathW(); - if (current_executable.empty()) { - return {}; - } - - return std::filesystem::path(current_executable).parent_path() / - L"crossdesk_service.exe"; + return crossdesk::platform::SiblingExecutablePath(L"crossdesk_service.exe"); } bool IsServiceCliCommand(const char* arg) { @@ -286,7 +272,7 @@ static int RunApplication(int argc, char* argv[]) { #ifdef _WIN32 crossdesk::get_logger()->info( "CrossDesk executable: {}", - std::filesystem::path(GetCurrentExecutablePathW()).u8string()); + std::filesystem::path(crossdesk::platform::CurrentExecutablePath()).u8string()); #endif if (IsDaemonChild(argc, argv)) { diff --git a/apps/desktop/src/platform/windows/autostart.cpp b/apps/desktop/src/platform/windows/autostart.cpp index 954bbc06..20705175 100644 --- a/apps/desktop/src/platform/windows/autostart.cpp +++ b/apps/desktop/src/platform/windows/autostart.cpp @@ -11,6 +11,7 @@ #include #include "autostart_task.h" +#include "executable_path.h" #include "rd_log.h" namespace crossdesk::platform { @@ -185,11 +186,7 @@ bool RemoveLogonTask(const std::wstring& app_name) { } // namespace std::string GetAutostartExecutablePath() { - std::vector path(32768); - const DWORD length = - GetModuleFileNameW(nullptr, path.data(), static_cast(path.size())); - if (length == 0 || length >= path.size()) return {}; - return std::filesystem::path(path.data(), path.data() + length).u8string(); + return std::filesystem::path(CurrentExecutablePath()).u8string(); } bool EnableAutostart(const std::string& app_name, diff --git a/apps/desktop/src/platform/windows/executable_path.h b/apps/desktop/src/platform/windows/executable_path.h new file mode 100644 index 00000000..a118fba9 --- /dev/null +++ b/apps/desktop/src/platform/windows/executable_path.h @@ -0,0 +1,30 @@ +#ifndef CROSSDESK_PLATFORM_WINDOWS_EXECUTABLE_PATH_H_ +#define CROSSDESK_PLATFORM_WINDOWS_EXECUTABLE_PATH_H_ + +#include + +#include +#include +#include + +namespace crossdesk::platform { + +// Full path of this process image, including long paths; empty on failure. +inline std::wstring CurrentExecutablePath() { + std::vector path(32768); + const DWORD length = GetModuleFileNameW(nullptr, path.data(), + static_cast(path.size())); + if (length == 0 || length >= path.size()) return {}; + return {path.data(), length}; +} + +// A file installed next to this executable; empty if the path is unknown. +inline std::filesystem::path SiblingExecutablePath(const wchar_t* name) { + const auto executable = CurrentExecutablePath(); + if (executable.empty()) return {}; + return std::filesystem::path(executable).parent_path() / name; +} + +} // namespace crossdesk::platform + +#endif diff --git a/apps/desktop/src/platform/windows/runtime_daemon.cpp b/apps/desktop/src/platform/windows/runtime_daemon.cpp index da198394..4c74803b 100644 --- a/apps/desktop/src/platform/windows/runtime_daemon.cpp +++ b/apps/desktop/src/platform/windows/runtime_daemon.cpp @@ -1,5 +1,7 @@ #include "platform/windows/runtime_daemon.h" +#include "platform/windows/executable_path.h" + #include #include @@ -25,14 +27,6 @@ struct Arguments { ~Arguments() { if (values) LocalFree(values); } }; -std::wstring ExecutablePath() { - std::vector path(32768); - const DWORD count = GetModuleFileNameW(nullptr, path.data(), - static_cast(path.size())); - if (!count || count >= path.size()) return {}; - return {path.data(), count}; -} - std::wstring QuoteArgument(const std::wstring& value) { std::wstring result = L"\""; size_t slashes = 0; @@ -130,7 +124,7 @@ WindowsRuntimeDaemon::~WindowsRuntimeDaemon() { Shutdown(); } void WindowsRuntimeDaemon::Initialize() { std::lock_guard lock(mutex_); initialized_ = true; - executable_ = ExecutablePath(); + executable_ = CurrentExecutablePath(); arguments_.clear(); Arguments arguments; if (!arguments.values) { @@ -273,7 +267,7 @@ int RunWindowsRuntimeDaemon() { return 1; } } - const auto executable = ExecutablePath(); + const auto executable = CurrentExecutablePath(); if (executable.empty()) return 1; std::wstring command = QuoteArgument(executable); for (int i = 5; i < arguments.count; ++i)