0
votes

Once installed the application using NSIS, To uninstall it from the Control Panel written the below script

Function un.onInit
    MessageBox MB_YESNO|MB_ICONEXCLAMATION "Are you sure you want to uninstall EMR?" /SD IDYES IDYES NoAbort
    Abort 
    NoAbort:
    SetAutoClose true

FunctionEnd

So that while uninstalling, first shows the pop-up message ("Are you sure you want to uninstall EMR?") When clicked on "OK", uninstallation completes.

And also to uninstall the installed software before installing it again, written the below script.

Function RemovePrevVerFunction

ReadRegStr $R0 HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\EMR"  "UninstallString" 

 ${If} $R0 != ""
 MessageBox MB_OKCANCEL "EMR is already installed. Do you want to remove the previous version?" IDOK uninst
 Abort
 uninst:
 ExecWait "$INSTDIR\uninstall.exe"
 Quit
FunctionEnd

In the above script i have not used ExecWait "$INSTDIR\uninstall.exe /S", because I wanted to show the uninstallation process to the user.

But here how to hide the message "Are you sure you want to uninstall EMR?" that is popping up from un.onInit?

When uninstalling MSI, we are using "/qb" to hide the message. Like this is there any way to hide the message using NSIS?

1

1 Answers

0
votes

A MessageBox can be skipped when using /S but without it you have to make your own detection logic:

!include LogicLib.nsh
!include FileFunc.nsh

InstallDir "$ProgramFiles\Test"

Page Directory
Page InstFiles

Function GetAppFromCommand
Exch $1
Push $2
StrCpy $2 $1 1 0
StrCmp $2 '"' 0 done
Push $3
StrCpy $3 ""
loop:
    IntOp $3 $3 + 1
    StrCpy $2 $1 1 $3
    StrCmp $2 '' +2
    StrCmp $2 '"' 0 loop
    StrCpy $1 $1 $3
    StrCpy $1 $1 "" 1 ; Remove starting quote
Pop $3
done:
Pop $2
Exch $1
FunctionEnd
!macro GetAppFromCommand in out
Push "${in}"
Call GetAppFromCommand
Pop ${out}
!macroend

!macro UninstallPreviousNSIS UninstCommand CustomParameters
Push $0
Push $1
Push $2
Push '${CustomParameters}'
Push '${UninstCommand}'
Call GetAppFromCommand ; Remove quotes and parameters from UninstCommand 
Pop $0
Pop $1
GetFullPathName $2 "$0\.."
ExecWait '"$0" $1 _?=$2'
Delete "$0" ; Extra cleanup because we used _?=
RMDir "$2"
Pop $2
Pop $1
Pop $0
!macroend

Function .onInit
ReadRegStr $R0 HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" "UninstallString"
${If} $R0 != ""
    MessageBox MB_YESNO|MB_ICONQUESTION "$(^Name) is already installed. Do you want to remove the previous version?" IDNO noUninstOld
    !insertmacro UninstallPreviousNSIS $R0 "/NoMsgBox"
    noUninstOld:
${EndIf}
FunctionEnd

Section
SetOutPath $InstDir
File "/oname=$InstDir\Dummy.txt" "${__FILE__}"
WriteUninstaller "$InstDir\Uninst.exe"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" "UninstallString" '"$InstDir\Uninst.exe"'
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" "DisplayName" "$(^Name)"
SectionEnd

Function un.onInit
${GetParameters} $0
ClearErrors
${GetOptions} "$0" "/NoMsgBox"  $1
${IfNotThen} ${Errors} ${|} Goto NoAbort ${|}
MessageBox MB_YESNO|MB_ICONEXCLAMATION "Are you sure you want to uninstall $(^Name)?" /SD IDYES IDYES NoAbort
Abort 
NoAbort:
FunctionEnd

Section Uninstall
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)"
Delete "$InstDir\Dummy.txt"
Delete "$InstDir\Uninst.exe"
RMDir "$InstDir"
SectionEnd