[feat] make CrossDesk service start and stop with the app

This commit is contained in:
dijunkun
2026-04-28 10:25:16 +08:00
parent 71bce08549
commit 5f541f5c8b
4 changed files with 198 additions and 16 deletions
+22 -4
View File
@@ -14,6 +14,7 @@
#ifdef _WIN32
#include <cstdio>
#include "service_host.h"
#endif
@@ -53,7 +54,8 @@ void PrintServiceCliUsage() {
<< " --service-uninstall Remove the installed Windows service\n"
<< " --service-start Start the Windows service\n"
<< " --service-stop Stop the Windows service\n"
<< " --service-sas Ask the service to send Secure Attention Sequence\n"
<< " --service-sas Ask the service to send Secure Attention "
"Sequence\n"
<< " --service-ping Ping the service over named pipe IPC\n"
<< " --service-status Query service runtime status\n"
<< " --service-help Show this help\n";
@@ -87,12 +89,25 @@ bool IsServiceCliCommand(const char* arg) {
std::strcmp(arg, "--service-uninstall") == 0 ||
std::strcmp(arg, "--service-start") == 0 ||
std::strcmp(arg, "--service-stop") == 0 ||
std::strcmp(arg, "--service-sas") == 0 ||
std::strcmp(arg, "--service-sas") == 0 ||
std::strcmp(arg, "--service-ping") == 0 ||
std::strcmp(arg, "--service-status") == 0 ||
std::strcmp(arg, "--service-help") == 0;
}
void TryStartManagedWindowsService() {
std::filesystem::path service_path = GetSiblingServiceExecutablePath();
if (service_path.empty() || !std::filesystem::exists(service_path)) {
return;
}
if (!crossdesk::IsCrossDeskServiceInstalled()) {
return;
}
crossdesk::StartCrossDeskService();
}
int HandleServiceCliCommand(const std::string& command) {
EnsureConsoleForCli();
@@ -120,8 +135,7 @@ int HandleServiceCliCommand(const std::string& command) {
if (command == "--service-uninstall") {
bool success = crossdesk::UninstallCrossDeskService();
std::cout << (success ? "uninstall ok" : "uninstall failed")
<< std::endl;
std::cout << (success ? "uninstall ok" : "uninstall failed") << std::endl;
return success ? 0 : 1;
}
@@ -182,6 +196,10 @@ int main(int argc, char* argv[]) {
return 0;
}
#ifdef _WIN32
TryStartManagedWindowsService();
#endif
bool enable_daemon = false;
auto path_manager = std::make_unique<crossdesk::PathManager>("CrossDesk");
if (path_manager) {