Skip to content

Commit 998e9a5

Browse files
authored
Merge pull request #9966 from f321x/improve_windows_installer_safety
block windows installer if Electrum is still running
2 parents a8b8359 + ddddaad commit 998e9a5

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

contrib/build-wine/electrum.nsi

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,55 @@
8787

8888
!insertmacro MUI_LANGUAGE "English"
8989

90+
;--------------------------------
91+
;Functions
92+
93+
!macro CreateEnsureNotRunning prefix operation
94+
95+
Function ${prefix}EnsureNotRunning
96+
; pop the directory to check from the stack into $R0
97+
Pop $R0
98+
; if the dir at $R0 doesn't exist, jump to nodir
99+
IfFileExists "$R0" 0 nodir
100+
; Find all .exe files in the directory, $1 is the handle, $2 is the filename
101+
FindFirst $1 $2 "$R0\*.exe"
102+
IfErrors noexe 0
103+
104+
checkloop:
105+
; Skip checking the uninstaller if we are the uninstaller to avoid locking the uninstaller itself
106+
!if "${prefix}" == "un."
107+
StrCmp $2 "Uninstall.exe" skipfile 0
108+
!endif
109+
110+
; Check if we can append to the .exe file. If we can't that means it is still running.
111+
retryopen:
112+
FileOpen $0 "$R0\$2" a
113+
IfErrors 0 closeexe
114+
MessageBox MB_RETRYCANCEL "Can not ${operation} because $2 is still running. Close it and retry." /SD IDCANCEL IDRETRY retryopen
115+
FindClose $1
116+
Abort
117+
closeexe:
118+
FileClose $0
119+
120+
skipfile:
121+
; Find next .exe file
122+
FindNext $1 $2
123+
IfErrors done 0
124+
Goto checkloop
125+
126+
done:
127+
FindClose $1
128+
129+
noexe:
130+
nodir:
131+
FunctionEnd
132+
133+
!macroend
134+
135+
; The function has to be created twice, once for the installer and once for the uninstaller
136+
!insertmacro CreateEnsureNotRunning "" "install"
137+
!insertmacro CreateEnsureNotRunning "un." "uninstall"
138+
90139
;--------------------------------
91140
;Installer Sections
92141

@@ -99,6 +148,14 @@ Function .onInit
99148
SetErrorLevel 740 ;ERROR_ELEVATION_REQUIRED
100149
Quit
101150
${EndIf}
151+
152+
; Check if already installed and ensure the process is not running if it is
153+
ReadRegStr $R0 HKCU "Software\${PRODUCT_NAME}" ""
154+
IfErrors noinstdir 0
155+
Push $R0
156+
Call EnsureNotRunning
157+
noinstdir:
158+
ClearErrors
102159
FunctionEnd
103160

104161
Section
@@ -175,3 +232,9 @@ Section "Uninstall"
175232
DeleteRegKey HKCU "Software\${PRODUCT_NAME}"
176233
DeleteRegKey HKCU "${PRODUCT_UNINST_KEY}"
177234
SectionEnd
235+
236+
Function UN.onInit
237+
; Ensure the process is not running in the uninstallation directory
238+
Push $INSTDIR
239+
Call un.EnsureNotRunning
240+
FunctionEnd

0 commit comments

Comments
 (0)