5

So far I've figured out how to detect a previous installation of my software by reading registry keys, and testing for whether the directory exists. (Both are well documented in the NSIS help file). Now I want to force the user to specify a different directory, if the application is previously installed. (Don't want to force uninstall by myself, because previous versions simply remove everything including saved data).

As far as I can see, there are predefined templates in MUI2.nsh for the license, install folder, progress indicator etc. How do I add a validation at this stage in the installer flow?

Update - Tried Paul's solution, but it doesn't work. At the top of the script, I've declared

!define MUI_PAGE_CUSTOMFUNCTION_LEAVE validateDirectory
!insertmacro MUI_PAGE_DIRECTORY

to refer to this function:

Function validateDirectory
ReadRegStr $R0 HKLM "SOFTWARE\Aadhaar Enrolment Client" "Installdir"
Pop $R0
StrCmp $R0 $OUTDIR +1 +3
MessageBox MB_ICONSTOP|MB_OK 'The directory $OUTDIR already exists.Please choose a different directory.'
Abort
FunctionEnd

This function displays the message, but doesn't abort. Moreover, if I click on 'back' in the directory selection page and again click forward, then it simply proceeds with the installation.

Rex
  • 801
  • 1
  • 17
  • 26
  • It's been a while... no solution to this? Especially one that doesn't use MUI? – e40 Apr 01 '15 at 17:24
  • None..And I've long since moved on from using NSIS. – Rex Apr 01 '15 at 17:39
  • 2
    I'll just mention that I did get it working, with the help of the NSIS forum. http://forums.winamp.com/showthread.php?t=380848 – e40 Apr 01 '15 at 19:33
  • Using MUI, Paul's solution did work perfectly for me (NSIS 2.3, Abort didn't cause any problems here) – Mallard Sep 02 '15 at 09:04

1 Answers1

5

You need to specify a "Leave" function for the directory page like this

!define MUI_PAGE_CUSTOMFUNCTION_LEAVE LeaveDirectory
!insertmacro MUI_PAGE_DIRECTORY

and this will call the function specified when the Next button is clicked.

Then create the LeaveDirectory function with the logic required to validate the directory selected and if the directory is determined to be invalid, simply call Abort in the function and the installer won't move on to the next step.

The documentation is on this page under the "Custom Functions" section but because you have to expand the "Page Custom Functions" heading is not obvious unfortunately.

Paul Hunt
  • 3,395
  • 2
  • 24
  • 39
  • Thanks. So if I do that will it remain on the directory screen until the user chooses a valid directory? – Rex Dec 09 '11 at 13:07
  • Correct. So long as you call Abort in the function when the directory is invalid then it won't move on to the next page – Paul Hunt Dec 09 '11 at 13:12
  • If I try this Abort doesn't work as described here, it puts the installer in a bad state where all user input is ignored, is passed to whatever is underneath the installer and it is relegated to a background process in task manager... but can still be switched to with Alt-Tab! Very strange... – jheriko Jan 06 '15 at 15:32
  • And this solution forces the use of MUI, which I haven't gone to yet. I would dearly love a solution to this. – e40 Apr 01 '15 at 17:23