mirror of
https://github.com/kunkundi/crossdesk.git
synced 2026-11-13 20:25:53 +08:00
[refactor] share Windows executable path helpers
This commit is contained in:
1 parent
81fc8463dc
commit
c179b3c243
4 files changed
+39
-32
No files matched your search
@@ -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