Skip to content

Commit bca4ca5

Browse files
enhance(nsis): use restart manager to close app (#14479)
* enhance(nsis): use restart manager to close app * Replace `nsis_tauri_utils::FindProcess` Use restart manager APIs manually * Use fully qualified path on CheckIfAppIsRunning * Add change file
1 parent e2c54be commit bca4ca5

3 files changed

Lines changed: 50 additions & 40 deletions

File tree

‎.changes/nsis-restart-manager.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-bundler": minor:enhance
3+
---
4+
5+
Switch to use restart manager to close running app, this makes it so that we send a `WM_ENDSESSION` signal to the app for it to gracefully shutdown

‎crates/tauri-bundler/src/bundle/windows/nsis/installer.nsi‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ ManifestDPIAwareness PerMonitorV2
2727
!include "FileAssociation.nsh"
2828
!include "Win\COM.nsh"
2929
!include "Win\Propkey.nsh"
30+
!include "Win\RestartManager.nsh"
3031
!include "StrFunc.nsh"
3132
${StrCase}
3233
${StrLoc}
@@ -642,7 +643,7 @@ Section Install
642643
!insertmacro NSIS_HOOK_PREINSTALL
643644
!endif
644645

645-
!insertmacro CheckIfAppIsRunning "${MAINBINARYNAME}.exe" "${PRODUCTNAME}"
646+
!insertmacro CheckIfAppIsRunning "$INSTDIR\${MAINBINARYNAME}.exe" "${PRODUCTNAME}"
646647

647648
; Copy main executable
648649
File "${MAINBINARYSRCPATH}"
@@ -779,7 +780,7 @@ Section Uninstall
779780
!insertmacro NSIS_HOOK_PREUNINSTALL
780781
!endif
781782

782-
!insertmacro CheckIfAppIsRunning "${MAINBINARYNAME}.exe" "${PRODUCTNAME}"
783+
!insertmacro CheckIfAppIsRunning "$INSTDIR\${MAINBINARYNAME}.exe" "${PRODUCTNAME}"
783784

784785
; Delete the app directory and its content from disk
785786
; Copy main executable

‎crates/tauri-bundler/src/bundle/windows/nsis/utils.nsh‎

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
${EndIf}
1919
!macroend
2020

21+
!define /ifndef ERROR_MORE_DATA 234
22+
2123
; Checks whether app is running or not and prompts to kill it.
22-
!macro CheckIfAppIsRunning executableName productName
24+
!macro CheckIfAppIsRunning executablePath productName
2325
!define UniqueID ${__LINE__}
2426

2527
; Replace {{product_name}} placeholder in the messages with the passed product name
@@ -30,44 +32,46 @@
3032
nsis_tauri_utils::StrReplace "$(failedToKillApp)" "{{product_name}}" "${productName}"
3133
Pop $R3
3234

33-
!if "${INSTALLMODE}" == "currentUser"
34-
nsis_tauri_utils::FindProcessCurrentUser "${executableName}"
35-
!else
36-
nsis_tauri_utils::FindProcess "${executableName}"
37-
!endif
38-
Pop $R0
39-
${If} $R0 = 0
40-
IfSilent kill_${UniqueID} 0
41-
${IfThen} $PassiveMode != 1 ${|} MessageBox MB_OKCANCEL $R2 IDOK kill_${UniqueID} IDCANCEL cancel_${UniqueID} ${|}
42-
kill_${UniqueID}:
43-
!if "${INSTALLMODE}" == "currentUser"
44-
nsis_tauri_utils::KillProcessCurrentUser "${executableName}"
45-
!else
46-
nsis_tauri_utils::KillProcess "${executableName}"
47-
!endif
48-
Pop $R0
49-
Sleep 500
50-
${If} $R0 = 0
51-
${OrIf} $R0 = 2
52-
Goto app_check_done_${UniqueID}
53-
${Else}
54-
IfSilent silent_${UniqueID} ui_${UniqueID}
55-
silent_${UniqueID}:
56-
System::Call 'kernel32::AttachConsole(i -1)i.r0'
57-
${If} $0 != 0
58-
System::Call 'kernel32::GetStdHandle(i -11)i.r0'
59-
System::call 'kernel32::SetConsoleTextAttribute(i r0, i 0x0004)' ; set red color
60-
FileWrite $0 "$R1$\n"
61-
${EndIf}
62-
Abort
63-
ui_${UniqueID}:
64-
Abort $R3
65-
${EndIf}
66-
cancel_${UniqueID}:
67-
Abort $R1
35+
!insertmacro RestartManager_StartSession $R0
36+
${If} $R0 != ""
37+
!insertmacro RestartManager_RegisterFile $R0 "${executablePath}"
38+
${If} $0 = 0
39+
System::Call 'RSTRTMGR::RmGetList(p R0, *i .r1, *i .r2, p 0, *i .r3) i .r0'
40+
; ERROR_SUCCESS means no running processes, ERROR_MORE_DATA means there're some
41+
${If} $0 = ${ERROR_MORE_DATA}
42+
IfSilent kill_${UniqueID} 0
43+
${IfThen} $PassiveMode != 1 ${|} MessageBox MB_OKCANCEL $R2 IDOK kill_${UniqueID} IDCANCEL cancel_${UniqueID} ${|}
44+
kill_${UniqueID}:
45+
; Call manually here to get the result
46+
; !insertmacro RestartManager_Shutdown $R0
47+
System::Call 'RSTRTMGR::RmShutdown(p R0, i ${RmForceShutdown}, p 0) i .r0'
48+
${If} $0 = 0
49+
Goto app_check_done_${UniqueID}
50+
${Else}
51+
IfSilent silent_${UniqueID} ui_${UniqueID}
52+
silent_${UniqueID}:
53+
System::Call 'kernel32::AttachConsole(i -1)i.r0'
54+
${If} $0 != 0
55+
System::Call 'kernel32::GetStdHandle(i -11)i.r0'
56+
System::call 'kernel32::SetConsoleTextAttribute(i r0, i 0x0004)' ; set red color
57+
FileWrite $0 "$R1$\n"
58+
${EndIf}
59+
!insertmacro RestartManager_EndSession $R0
60+
Abort
61+
ui_${UniqueID}:
62+
!insertmacro RestartManager_EndSession $R0
63+
Abort $R3
64+
${EndIf}
65+
cancel_${UniqueID}:
66+
!insertmacro RestartManager_EndSession $R0
67+
Abort $R1
68+
app_check_done_${UniqueID}:
69+
${EndIf}
70+
${EndIf}
71+
!insertmacro RestartManager_EndSession $R0
6872
${EndIf}
69-
app_check_done_${UniqueID}:
70-
!undef UniqueID
73+
74+
!undef UniqueID
7175
!macroend
7276

7377
; Sets AppUserModelId on a shortcut

0 commit comments

Comments
 (0)