Set value of InstallDir in a function, or set auto populate value somehow? - nsis

I'm creating an installer using NSIS. This installer actually installs two programs in two different directories in the same installer. I am doing this using the modern user interface (MUI) pages and simply calling MUI_PAGE_DIRECTORY twice specifying different starting parameteres, and capturing the directory in the LEAVE macro. What I'm wondering is, can I somehow call InstallDir in a function, or set the auto directory populate value in a function? Or possibly even call a function after the browse button has been returned from?
The reason I want to do this is so when the user clicks the browse button in either of the two directory pages, after they select a directory, the name of the finnal directory specifed in InstallDir will be appended.
For example:
InstallDir value for program 1: c:\client
InstallDir value for program 2: c:\program files\server
user clicks browse on program 1 and chooses c:\temp the resulting path is c:\temp\client
user clicks browse on program 2 and chooses c:\whatever the resulting path is c:\whatever\server
For reference here are the code snipits of what I have that works, but does not deal with the auto append browse button behaviour:
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE ClientDirectoryLeave
!insertmacro MUI_PAGE_DIRECTORY
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE ServerDirectoryLeave
!insertmacro MUI_PAGE_DIRECTORY
; Setup the page display for the client install page
Function ShowPageClient
!insertmacro MUI_HEADER_TEXT "Client" "Client"
!insertmacro MUI_INNERDIALOG_TEXT 1006 "Client"
; setup intal directory
Push $0
StrCpy $0 $PROGRAMFILES 2 #
; CLIENT_FOLDER_NAME is defined as a folder, but this would basicaly
; result in C:\Client as the first 2 characters of $PROGRAMFILES
; is the hard drive with program files installed on it
StrCpy $INSTDIR "$0\${CLIENT_FOLDER_NAME}"
Pop $0
; set the inital value of the directory text box
!insertmacro MUI_INNERDIALOG_TEXT 1019 $INSTDIR
; find and disable the directory selection box
; We do not want users to type in this box
FindWindow $R0 "#32770" "" $HWNDPARENT
GetDlgItem $R1 $R0 1019 ;Text Box
EnableWindow $R1 0
FunctionEnd
; Setup the page display for the server install location page
Function ShowPageServer
!insertmacro MUI_HEADER_TEXT "Server" "Server"
!insertmacro MUI_INNERDIALOG_TEXT 1006 "Server"
; setup intal directory
; SERVER_FOLDER_NAME is defined as a folder, but this would basicaly
; result in C:\Program Files\Server
StrCpy $INSTDIR "$PROGRAMFILES\${SERVER_FOLDER_NAME}"
; set the inital value of the directory text box
!insertmacro MUI_INNERDIALOG_TEXT 1019 $INSTDIR
; find and disable the directory selection box
; We do not want users to type in this box
FindWindow $R0 "#32770" "" $HWNDPARENT
GetDlgItem $R1 $R0 1019 ;Text Box
EnableWindow $R1 0
FunctionEnd
Note: I can make the browse button work for one of the directory pages, but then when I'm on the second page, the auto populate actual auto populates incorrectly

The appended folder name is constant and set at compile time, there is a bug report related to this.
My advice is to abandon the append feature and let the user have full control over the two destinations:
Name "NSIS Test"
InstallDir ""
!include MUI.nsh
Var DirClient
Var DirServer
Function .onInit
;Set default destinations
StrCpy $DirClient "$ProgramFiles\$(^Name)\Client"
StrCpy $DirServer "$ProgramFiles\$(^Name)\Server"
FunctionEnd
!macro ConfigureMyDirPage type var
!define MUI_DIRECTORYPAGE_VARIABLE ${var}
!define MUI_PAGE_HEADER_SUBTEXT "Choose the folder in which to install $(^NameDA) ${type}"
!define MUI_DIRECTORYPAGE_TEXT_TOP "Setup will install $(^NameDA) ${type} in the following folder. To install in a different folder, click Browse and select another folder. $_CLICK"
!define MUI_DIRECTORYPAGE_TEXT_DESTINATION "${type} $(^DirSubText)"
!macroend
!insertmacro ConfigureMyDirPage "Client" $DirClient
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro ConfigureMyDirPage "Server" $DirServer
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"
Section
DetailPrint DirClient=$DirClient
DetailPrint DirServer=$DirServer
SectionEnd

Okay I finally figured it out. Basically there is a function that is called to "verify" the path after browse button is clicked. I tied into this function an appended the directory manual if needed. To do this I created a new variable and set it in a function called when the page is displays as follows:
; Client Directory
!define MUI_PAGE_CUSTOMFUNCTION_SHOW ShowPageClient
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE ClientDirectoryLeave
!insertmacro MUI_PAGE_DIRECTORY
; Setup the page display for the client install page
Function ShowPageClient
; setup intal directory
Push $0
StrCpy $0 $PROGRAMFILES 2 #
StrCpy $INSTDIR "$0\${CLIENT_FOLDER_NAME}"
Pop $0
!insertmacro MUI_INNERDIALOG_TEXT 1019 $INSTDIR
FindWindow $R0 "#32770" "" $HWNDPARENT
GetDlgItem $R1 $R0 1019 ;Text Box
EnableWindow $R1 0
; Setup the client or server variable to indicate that
; we're in the client install part to signal the .onVerifyInstDir function
; to put the correct directory
StrCpy $CLIENT_OR_SERVER "client"
FunctionEnd
The function that is called after browse is .onVerifyInstDir so that is where I check the CLIENT_OR_SERVER variable and set the path appropriately
; This function is special and is called any time a
; path is validated on returning from the browse button
; need to append the correct directory if it does not already exists
; in the install directory path
Function .onVerifyInstDir
; save the current $0 register, as it is used in this function
Push $0
${If} $CLIENT_OR_SERVER == "client"
; in the client stage so directory must contain CLIENT_FOLDER_NAME
${StrContains} $0 "${CLIENT_FOLDER_NAME}" "$INSTDIR"
${If} $0 == ""
; the install dir does not contain the folder so append it
StrCpy $INSTDIR "$INSTDIR\${CLIENT_FOLDER_NAME}"
${EndIf}
${Else}
; in the server stage so directory must contain SERVER_FOLDER_NAME
${StrContains} $0 "${SERVER_FOLDER_NAME}" "$INSTDIR"
${If} $0 == ""
; the install dir does not contain the folder so append it
StrCpy $INSTDIR "$INSTDIR\${SERVER_FOLDER_NAME}"
${EndIf}
${EndIf}
; pop the saved register value
Pop $0
FunctionEnd
Couple notes:
the StrContains function I used was found here:
http://nsis.sourceforge.net/StrContains
Further reference to the .onVerifyInstDir function can be found here:
http://nsis.sourceforge.net/Docs/Chapter4.html#4.7.2.1.10

Related

RelGotoPage function goes to next page instead of going back

My current defined order of pages in the Installer are:
!insertmacro MUI_PAGE_WELCOME ; Welcome page
!insertmacro MUI_PAGE_DIRECTORY ; Select a directory
!define MUI_PAGE_CUSTOMFUNCTION_PRE CheckInstallDirectory ; Check selected directory
!insertmacro MUI_PAGE_COMPONENTS ; Choose install components
!define MUI_PAGE_CUSTOMFUNCTION_PRE SkipLicense ; Component1's check before license page
!insertmacro MUI_PAGE_LICENSE "license.txt" ; Component1's license
!define MUI_PAGE_CUSTOMFUNCTION_PRE SkipLicenseForComp2 ; Component2's check before license page
!insertmacro MUI_PAGE_LICENSE "License2.txt" ; Component2's license
!insertmacro CUSTOM_PAGES ; Custom install pages
!insertmacro MUI_PAGE_INSTFILES ; Install components' files
!insertmacro MUI_PAGE_FINISH ; Finish page
In the CheckInstallDirectory function, which is defined below, it checks if the end-user selected a directory that already houses an .exe that would otherwise be installed by the installer.
If the directory exists, the user is presented a pop-up message box with the "OK" and "CANCEL" buttons.
"OK" selection should simply skip the next few pages and go to MUI_PAGE_INSTFILES page.
"CANCEL" selection should return the user to the MUI_PAGE_DIRECTORY page.
Function CheckInstallDirectory
IfFileExists "$INSTDIR\Component1.exe" file_found end_of_check
file_found:
Var /GLOBAL ver_
${GetFileVersion} "$INSTDIR\Component1.exe" $ver_
StrCpy $0 $ver_ 1 ; Determine the major version of the installed exe (1st char of the returned string)
StrCpy $1 $ver_1 1 ; Determine the major version of the exe we are installing (1st char of the returned string)
${If} $0 == $1
; The major version between .exe's is the same
MessageBox MB_OKCANCEL|MB_ICONEXCLAMATION "A copy of this software is already installed in the selected directory.$\nSelect $\"OK$\" if you would like to update the software to the latest version.$\nOtherwise select $\"Cancel$\" to go back and select a different installation directory." IDOK OK IDCANCEL CANCEL
OK:
; User selected to update this software - continue with the installation like normal
StrCpy $R9 "7" ; Skip to the installation page
Call RelGotoPage
goto end_of_check
CANCEL:
StrCpy $R9 "-1" ; Go back to directory selection page
Call RelGotoPage
goto end_of_check
${Else}
; The major version between the .exe's is NOT the same - cannot update
MessageBox MB_OK "A copy of this software is already installed in the selected directory.$\nIt is not part of the latest major-version group and cannot be updated. To update this installation, uninstall and use the installer to install the latest version.$\nOtherwise, select$\"OK$\" to go back and select a different installation directory."
StrCpy $R9 "-1" ; Go back to directory selection page
Call RelGotoPage
goto end_of_check
${EndIf}
end_of_check:
FunctionEnd
I have found NSIS's description for how to move across installer pages here.
Function RelGotoPage
IntCmp $R9 0 0 Move Move
StrCmp $R9 "X" 0 Move
StrCpy $R9 "120"
Move:
SendMessage $HWNDPARENT "0x408" "$R9" ""
FunctionEnd
The problem that is occurring is that StrCpy $R9 "7" does successfully skip to the MUI_PAGE_INSTFILES page. Yet, StrCpy $R9 "-1" does not return to the previous page (which should be MUI_PAGE_DIRECTORY). Instead, if "CANCEL" is selected in that pop-up message, the installer goes to the next page, MUI_PAGE_COMPONENTS.
What is incorrect here?
Is there a better way to achieve the desired result?
Any help is sincerely appreciated! Thank you!
In the page pre-callback the page is not fully loaded and I assume that is why -1 does not work correctly. -1 does work if you use MUI_PAGE_CUSTOMFUNCTION_SHOW but the proper way to do this is to block the page change:
!include MUI2.nsh
!insertmacro MUI_PAGE_WELCOME ; Welcome page
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE CheckInstallDirectory
!insertmacro MUI_PAGE_DIRECTORY
...
Function CheckInstallDirectory
;Snipped
MessageBox MB_OKCANCEL|MB_ICONEXCLAMATION "A copy of this software is already installed in the selected directory.$\nSelect $\"OK$\" if you would like to update the software to the latest version.$\nOtherwise select $\"Cancel$\" to go back and select a different installation directory." IDOK OK IDCANCEL CANCEL
OK:
; User selected to update this software - continue with the installation like normal
StrCpy $R9 "7" ; Skip to the installation page
Call RelGotoPage
goto end_of_check
CANCEL:
Abort
;Snipped
end_of_check:
FunctionEnd

How to load the files based on the locale and generate the installer using NSIS?

My requirement is, I have specific files (Dlls, chms, etc) for each Locale (Language). I need to load those files based on the locale and generate the installer.
And while uninstalling i should uninstall those files as well from the traget directory.
Here what i am doing is in .onInit function, using GetUserDefaultUILanguage() i am getting the locale and checking this locale and loading the files under this locale.
is it the correct way? Please provide any suggestions for this code.
And also do we need to use ;Pages section before the ;Languages section?
Because i am getting a warning to use ;Pages section before the ;Languages section when i am compiling.
Below is the code snippet i have written:
; LocaleDlls.nsi
;
;
; It will install LocaleDlls.nsi into a directory that the user selects.
;--------------------------------
!include LogicLib.nsh
!include "MUI2.nsh"
; The name of the installer in the path C:\Program Files\LocaleDlls
Name "LocaleDlls"
; The file to write in the path E:\Source\NULLSOFT\src
OutFile "LocaleDlls.exe"
; The default installation directory in the path C:\Program Files\LocaleDlls
InstallDir $PROGRAMFILES\LocaleDlls
; Registry key to check for directory (so if you install again, it will
; overwrite the old one automatically) It shows the path the path C:\Program Files\LocaleDlls
InstallDirRegKey HKLM "Software\NSIS_LocaleDlls" "Install_Dir"
; Request application privileges for Windows Vista
RequestExecutionLevel admin
;--------------------------------
; Pages
Page components
Page directory
Page instfiles
UninstPage uninstConfirm
UninstPage instfiles
;Pages
; Do we need to use PAGE macros before giving LANGUAGE as when compiling we are getting an error.
;--------------------------------
;Languages
!insertmacro MUI_LANGUAGE "English" ; The first language is the default language
!insertmacro MUI_LANGUAGE "PortugueseBR"
;--------------------------------
;Installer Functions
Function .onInit
System::Call 'kernel32::GetUserDefaultUILanguage() i.r10'
MessageBox MB_OK "Return value = $R0"
StrCpy $Language ${LANG_PORTUGUESEBR}
MessageBox MB_OK "Return value = $Language"
${If} $Language P= 1046
MessageBox MB_OK "Current Locale is Portuguese... Loading Portuguese Files"
${EndIf}
File E:\Source\NULLSOFT\src\EngPortuguese\Portuguese\AllowStandby.reg
File E:\Source\NULLSOFT\src\EngPortuguese\Portuguese\Test.chm
File E:\Source\NULLSOFT\src\EngPortuguese\Portuguese\Testdlg.dll
File E:\Source\NULLSOFT\src\EngPortuguese\Portuguese\resource.dll
FunctionEnd
;--------------------------------
; The stuff to install
Section "LocaleDlls (required)"
SectionIn RO
; Set output path to the installation directory. Here is the path C:\Program Files\LocaleDlls
SetOutPath $INSTDIR
; Give the File path
System::Call 'KERNEL32::AddDllDirectory(w "$INSTDIR")' ; Tell Windows we trust all .DLLs in this directory
System::Call 'KERNEL32::LoadLibrary(t "$INSTDIR\testdlg.dll.dll")p.r8 ?e'
Pop $7 ; Get ?e result
${IfThen} $8 P= 0 ${|} MessageBox MB_ICONSTOP "Failed to load pchuteres.dll, error $7" ${|}
${If} $8 P<> 0
MessageBox MB_OK 'Successfully loaded "$INSTDIR\testdlg.dll.dll" # $8'
${EndIf}
; Do the install
; Write the installation path into the registry
WriteRegStr HKLM SOFTWARE\NSIS_DllTesting "Install_Dir" "$INSTDIR"
; Write the uninstall keys for Windows
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\LocaleDlls" "DisplayName" "NSIS LocaleDlls"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\LocaleDlls" "UninstallString" '"$INSTDIR\uninstall.exe"'
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\LocaleDlls" "NoModify" 1
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\LocaleDlls" "NoRepair" 1
WriteUninstaller "uninstall.exe"
SectionEnd
; Optional section (can be disabled by the user)
Section "Start Menu Shortcuts"
CreateDirectory "$SMPROGRAMS\LocaleDlls"
CreateShortcut "$SMPROGRAMS\LocaleDlls\Uninstall.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0
CreateShortcut "$SMPROGRAMS\LocaleDlls\LocaleDlls (MakeNSISW).lnk" "$INSTDIR\LocaleDlls.nsi" "" "$INSTDIR\LocaleDlls.nsi" 0
SectionEnd
;--------------------------------
; Uninstaller
Section "Uninstall"
; Remove registry keys
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\LocaleDlls"
DeleteRegKey HKLM SOFTWARE\NSIS_LocaleDlls
; Remove files and uninstaller
Delete $INSTDIR\LocaleDlls.nsi
Delete $INSTDIR\uninstall.exe
; Remove shortcuts, if any
Delete "$SMPROGRAMS\LocaleDlls\*.*"
; Remove directories used
RMDir "$SMPROGRAMS\LocaleDlls"
RMDir "$INSTDIR"
SectionEnd
;--------------------------------
;Uninstaller Functions
Function un.onInit
!insertmacro MUI_UNGETLANGUAGE
FunctionEnd
You don't need to call GetUserDefaultUILanguage, NSIS calls GetUserDefaultUILanguage to try to set the default $language. If it cannot find a matching language, the first language specified in the .NSI is used. All of this happens before .onInit is called and you don't have to do anything. You can however change $language in .onInit if you are not happy with the language NSIS has chosen. You can also use !insertmacro MUI_LANGDLL_DISPLAY to display a language selection dialog.
Pages need to be inserted before the language when using MUI because the language macro needs to know which strings are required by the pages. It is important that you use the MUI page macros MUI_PAGE_* and not the native page:
!insertmacro MUI_PAGE_WELCOME
Page Custom MyPage ; There is no MUI_PAGE_* macro for this
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English" ; Must come after all MUI_PAGE_* macros.

How to display install page with the copy process as like in the image [NSIS script]

Outfile Test.exe
name "Test"
!include MUI2.nsh
!include LogicLib.nsh
!insertmacro MUI_PAGE_WELCOME
Page instfiles Installer
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"
Function Installer
UserInfo::GetAccountType
pop $0
${If} $0 != "admin" ;Require admin rights to install application
MessageBox mb_iconstop "Administrator rights required!"
SetErrorLevel 740 ;ERROR_ELEVATION_REQUIRED
Quit
${Else}
System::Call "kernel32::GetCurrentDirectory(i ${NSIS_MAX_STRLEN}, t .r0)"
CreateDirectory $3\pj
CopyFiles /SILENT \Source\*.* \destination\
${EndIf}
FunctionEnd
You have two InstFiles pages in your script, don't do that. You are also calling a function at the start of the InstFiles page but you should not perform file operations in page functions.
Your file operation itself makes no sense, you should not copy based on the current directory!
The "copy" progress from your screenshot is actually extracting files from the installer and you get that for free in NSIS:
RequestExecutionLevel Admin
InstallDir "$ProgramFiles\MyApp"
!include MUI2.nsh
!include LogicLib.nsh
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"
Function .onInit
UserInfo::GetAccountType
pop $0
${If} $0 != "admin" ;Require admin rights to install application
MessageBox mb_iconstop "Administrator rights required!"
SetErrorLevel 740 ;ERROR_ELEVATION_REQUIRED
Quit
${EndIf}
FunctionEnd
Section
SetOutPath "$InstDir"
File /r "c:\myfiles" ; This will display the extract progress
SectionEnd
If you actually want to copy files instead of extracting then you can just remove the /SILENT switch to display the normal Windows copy dialog.
If you must copy files with output similar to extraction you must manually walk the source directory with FindFirst+FindNext and use DetailPrint+CopyFiles /SILENT for each file...

Customizing the NSIS Uninstaller Confirm Page

I've done some research on customizing NSIS uninstaller pages and had some success with the Welcome and Finish pages.
However I'm having trouble using the same template as I did on the Welcome page with the Confirm page. If I add any control using nsDialogs with a non-zero height value, all of the existing controls (apart from the header and the buttons) on the Confirm page disappear.
Here is my code (it successfully updates the button name but will remove all other controls on that page)
!include MUI2.nsh
OutFile "CustomUninstaller.exe"
InstallDir "C:\Program Files (x86)\NSIS\Examples\CustomFinish"
!define APPNAME "Testing"
Name "${APPNAME}"
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_UNPAGE_WELCOME
!define MUI_PAGE_CUSTOMFUNCTION_SHOW un.ModifyUnConfirm ; My custom function for the Confirm page
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_UNPAGE_FINISH
!insertmacro MUI_LANGUAGE "English"
Function un.ModifyUnConfirm
# Change "Uninstall" button to say "Continue" on uninstaller confirmation page
GetDlgItem $0 $HWNDPARENT 1
SendMessage $0 ${WM_SETTEXT} 0 "STR:Continue"
# Add new label. If these three lines are commented out, I see the regular controls of this page show up.
# The position, color and background of the label don't seem to matter
${NSD_CreateLabel} 50% 50% 100% 10u "Testing!!!"
Pop $0
SetCtlColors $0 "" ${MUI_BGCOLOR}
FunctionEnd
Section TestUninstaller
WriteUninstaller "$INSTDIR\unCustomUninstaller.exe"
ExecWait "$INSTDIR\unCustomUninstaller.exe"
Sectionend
Questions:
Does anyone know why the controls disappear?
What do I need to do to successfully add a control to this page?
MUI_UNPAGE_CONFIRM is not a nsDialogs page!
To change this dialog you can use the ChangeUI command.
You can also add controls at runtime but only by using the system plugin:
Function un.ModifyUnConfirm
FindWindow $1 "#32770" "" $HWNDPARENT ; Find inner dialog
System::Call 'USER32::CreateWindowEx(i${__NSD_Label_EXSTYLE},t"${__NSD_Label_CLASS}",t "Testing!!!",i${__NSD_Label_STYLE},i 50,i 100,i 400, i 25,i$1,i0,i0,i0)i.s'
Pop $0
SendMessage $HWNDPARENT ${WM_GETFONT} 0 0 $1
SendMessage $0 ${WM_SETFONT} $1 1
SetCtlColors $0 "" ${MUI_BGCOLOR} ; This is the wrong color to use...
FunctionEnd

How to call directory page inside the function in NSIS?

I have created Exe file for my app using NSIS script.In my script i have checked free space for selected directory.
1.If selected directory dont have required space then user wants to change the directory.
2.After changing directory in directory page again wants to check free space.so when the required space is available for the selected directory then only proceed to next page.
So it will come under looping statement.I have tried following script
page custom checking
Function checking
Push "\"
push $InstallDir
Call SplitFirstStrPart
pop $R0
${DriveSpace} $R0 "/D=F /S=G" $R0
${While} $R0 <= 2
MessageBox MB_OK "Expected free space is not availble"
call directory
${EndWhile}
Function directory
--Here i want to define directory page--
[page directory] we cant use this here
call checking
FunctionEnd
1.How to create user defined directory page?
2.Is possible to call page directory or MUI_PAGE_DIRECTORY multiple times?
Thanks
You cannot call a page from a function but you can skip a page by calling Abort in the page PRE callback and you can also jump to any page.
You can have multiple pages of all page types:
!include MUI.nsh
Var dir1
Var dir2
Function .onInit
StrCpy $dir1 c:\default1
StrCpy $dir2 c:\default2
FunctionEnd
!define MUI_DIRECTORYPAGE_VARIABLE $dir1
!insertmacro MUI_PAGE_DIRECTORY
!define MUI_DIRECTORYPAGE_VARIABLE $dir2
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE English
Section
DetailPrint $dir1
DetailPrint $dir2
SectionEnd

Resources