Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSIS - force user to select a different directory

Tags:

windows

nsis

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.

like image 774
Rex Avatar asked Dec 09 '11 12:12

Rex


1 Answers

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.

like image 83
Paul Hunt Avatar answered Oct 12 '22 07:10

Paul Hunt