diff --git a/scripts/windows/nsis_script.nsi b/scripts/windows/nsis_script.nsi index 111b2f2..8c456ce 100644 --- a/scripts/windows/nsis_script.nsi +++ b/scripts/windows/nsis_script.nsi @@ -8,6 +8,7 @@ !define PRODUCT_WEB_SITE "https://www.crossdesk.cn/" !define APP_NAME "CrossDesk" !define UNINSTALL_REG_KEY "CrossDesk" +!define PRODUCT_SERVICE_NAME "CrossDeskService" ; Installer icon path !define MUI_ICON "${__FILEDIR__}\..\..\icons\windows\crossdesk.ico" @@ -68,14 +69,21 @@ cancelInstall: Abort installApp: + Call StopInstalledService + SetOutPath "$INSTDIR" SetOverwrite ifnewer ; Main application executable path File /oname=CrossDesk.exe "..\..\build\windows\x64\release\crossdesk.exe" + ; Bundle service-side binaries required by the Windows service flow + File "..\..\build\windows\x64\release\crossdesk_service.exe" + File "..\..\build\windows\x64\release\crossdesk_session_helper.exe" ; Bundle runtime DLLs from the release output directory File "..\..\build\windows\x64\release\*.dll" + Call RegisterInstalledService + ; Write uninstall information WriteUninstaller "$INSTDIR\uninstall.exe" @@ -122,8 +130,12 @@ cancelUninstall: Abort uninstallApp: + Call UnregisterInstalledService + ; Delete main executable and uninstaller Delete "$INSTDIR\CrossDesk.exe" + Delete "$INSTDIR\crossdesk_service.exe" + Delete "$INSTDIR\crossdesk_session_helper.exe" Delete "$INSTDIR\uninstall.exe" ; Recursively delete installation directory @@ -148,3 +160,85 @@ SectionEnd Function LaunchApp Exec "$INSTDIR\CrossDesk.exe" FunctionEnd + +Function StopInstalledService + IfFileExists "$INSTDIR\CrossDesk.exe" 0 stop_with_sc + IfFileExists "$INSTDIR\crossdesk_service.exe" 0 stop_with_sc + + DetailPrint "Stopping existing CrossDesk service" + ExecWait '"$INSTDIR\CrossDesk.exe" --service-stop' $0 + ${If} $0 = 0 + Return + ${EndIf} + +stop_with_sc: + DetailPrint "Stopping existing CrossDesk service via Service Control Manager" + ExecWait '"$SYSDIR\sc.exe" stop ${PRODUCT_SERVICE_NAME}' $0 + ${If} $0 != 0 + ${AndIf} $0 != 1060 + ${AndIf} $0 != 1062 + MessageBox MB_ICONSTOP|MB_OK "Failed to stop the existing CrossDesk service. The installation will be aborted." + Abort + ${EndIf} + Sleep 1500 +FunctionEnd + +Function RegisterInstalledService + IfFileExists "$INSTDIR\CrossDesk.exe" 0 missing_service_binary + IfFileExists "$INSTDIR\crossdesk_service.exe" 0 missing_service_binary + IfFileExists "$INSTDIR\crossdesk_session_helper.exe" 0 missing_service_binary + + DetailPrint "Registering CrossDesk service" + ExecWait '"$INSTDIR\CrossDesk.exe" --service-install' $0 + ${If} $0 != 0 + MessageBox MB_ICONSTOP|MB_OK "Failed to register the CrossDesk service. The installation will be aborted." + Abort + ${EndIf} + + DetailPrint "Starting CrossDesk service" + ExecWait '"$INSTDIR\CrossDesk.exe" --service-start' $0 + ${If} $0 != 0 + ExecWait '"$INSTDIR\CrossDesk.exe" --service-uninstall' $1 + ExecWait '"$SYSDIR\sc.exe" delete ${PRODUCT_SERVICE_NAME}' $1 + MessageBox MB_ICONSTOP|MB_OK "The CrossDesk service was registered but could not be started. The installation will be aborted." + Abort + ${EndIf} + + Return + +missing_service_binary: + MessageBox MB_ICONSTOP|MB_OK "CrossDesk service files are missing from the installer package. The installation will be aborted." + Abort +FunctionEnd + +Function UnregisterInstalledService + IfFileExists "$INSTDIR\CrossDesk.exe" 0 unregister_with_sc + + DetailPrint "Stopping CrossDesk service" + ExecWait '"$INSTDIR\CrossDesk.exe" --service-stop' $0 + ${If} $0 = 0 + DetailPrint "Removing CrossDesk service" + ExecWait '"$INSTDIR\CrossDesk.exe" --service-uninstall' $0 + ${If} $0 = 0 + Return + ${EndIf} + ${EndIf} + +unregister_with_sc: + DetailPrint "Removing CrossDesk service via Service Control Manager" + ExecWait '"$SYSDIR\sc.exe" stop ${PRODUCT_SERVICE_NAME}' $0 + ${If} $0 != 0 + ${AndIf} $0 != 1060 + ${AndIf} $0 != 1062 + MessageBox MB_ICONSTOP|MB_OK "Failed to stop the CrossDesk service. Uninstall will be aborted." + Abort + ${EndIf} + Sleep 1500 + + ExecWait '"$SYSDIR\sc.exe" delete ${PRODUCT_SERVICE_NAME}' $0 + ${If} $0 != 0 + ${AndIf} $0 != 1060 + MessageBox MB_ICONSTOP|MB_OK "Failed to remove the CrossDesk service. Uninstall will be aborted." + Abort + ${EndIf} +FunctionEnd diff --git a/src/service/windows/interactive_state.h b/src/service/windows/interactive_state.h index 8476983..0a2a9cd 100644 --- a/src/service/windows/interactive_state.h +++ b/src/service/windows/interactive_state.h @@ -1,5 +1,11 @@ -#ifndef _CROSSDESK_INTERACTIVE_STATE_H_ -#define _CROSSDESK_INTERACTIVE_STATE_H_ +/* + * @Author: DI JUNKUN + * @Date: 2026-04-21 + * Copyright (c) 2026 by DI JUNKUN, All Rights Reserved. + */ + +#ifndef _INTERACTIVE_STATE_H_ +#define _INTERACTIVE_STATE_H_ #include diff --git a/src/service/windows/service_host.h b/src/service/windows/service_host.h index 726029d..f851ffb 100644 --- a/src/service/windows/service_host.h +++ b/src/service/windows/service_host.h @@ -1,5 +1,11 @@ -#ifndef _CROSSDESK_SERVICE_HOST_H_ -#define _CROSSDESK_SERVICE_HOST_H_ +/* + * @Author: DI JUNKUN + * @Date: 2026-04-21 + * Copyright (c) 2026 by DI JUNKUN, All Rights Reserved. + */ + +#ifndef _SERVICE_HOST_H_ +#define _SERVICE_HOST_H_ #include diff --git a/src/service/windows/session_helper_main.cpp b/src/service/windows/session_helper_main.cpp index 126f0ad..7d55859 100644 --- a/src/service/windows/session_helper_main.cpp +++ b/src/service/windows/session_helper_main.cpp @@ -1,5 +1,7 @@ -#include +// clang-format off #include +#include +// clang-format on #include #include #include diff --git a/src/service/windows/session_helper_shared.h b/src/service/windows/session_helper_shared.h index b68e8f5..1dabd24 100644 --- a/src/service/windows/session_helper_shared.h +++ b/src/service/windows/session_helper_shared.h @@ -1,5 +1,11 @@ -#ifndef _CROSSDESK_SESSION_HELPER_SHARED_H_ -#define _CROSSDESK_SESSION_HELPER_SHARED_H_ +/* + * @Author: DI JUNKUN + * @Date: 2026-04-21 + * Copyright (c) 2026 by DI JUNKUN, All Rights Reserved. + */ + +#ifndef _SESSION_HELPER_SHARED_H_ +#define _SESSION_HELPER_SHARED_H_ #include diff --git a/xmake/targets.lua b/xmake/targets.lua index 484af70..206054b 100644 --- a/xmake/targets.lua +++ b/xmake/targets.lua @@ -192,7 +192,7 @@ function setup_targets() add_files("src/service/windows/service_host.cpp") add_includedirs("src/service/windows", {public = true}) add_links("Advapi32", "Wtsapi32", "Ole32", "Userenv") - add_deps("wgc_plugin") + add_deps("wgc_plugin", "crossdesk_service", "crossdesk_session_helper") add_files("scripts/windows/crossdesk.rc") end end \ No newline at end of file