mirror of
https://github.com/kunkundi/crossdesk.git
synced 2026-11-11 02:06:38 +08:00
[refactor] share Windows executable path helpers
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
#include <cstdio>
|
||||
|
||||
#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)) {
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <vector>
|
||||
|
||||
#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<wchar_t> path(32768);
|
||||
const DWORD length =
|
||||
GetModuleFileNameW(nullptr, path.data(), static_cast<DWORD>(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,
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
#ifndef CROSSDESK_PLATFORM_WINDOWS_EXECUTABLE_PATH_H_
|
||||
#define CROSSDESK_PLATFORM_WINDOWS_EXECUTABLE_PATH_H_
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace crossdesk::platform {
|
||||
|
||||
// Full path of this process image, including long paths; empty on failure.
|
||||
inline std::wstring CurrentExecutablePath() {
|
||||
std::vector<wchar_t> path(32768);
|
||||
const DWORD length = GetModuleFileNameW(nullptr, path.data(),
|
||||
static_cast<DWORD>(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
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "platform/windows/runtime_daemon.h"
|
||||
|
||||
#include "platform/windows/executable_path.h"
|
||||
|
||||
#include <shellapi.h>
|
||||
|
||||
#include <cerrno>
|
||||
@@ -25,14 +27,6 @@ struct Arguments {
|
||||
~Arguments() { if (values) LocalFree(values); }
|
||||
};
|
||||
|
||||
std::wstring ExecutablePath() {
|
||||
std::vector<wchar_t> path(32768);
|
||||
const DWORD count = GetModuleFileNameW(nullptr, path.data(),
|
||||
static_cast<DWORD>(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<std::mutex> 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)
|
||||
|
||||
Reference in New Issue
Block a user