is it possible in nsis from the dialog to pass the input value as a parameter to the called console application through the installer, - nsis

is it possible in nsis from the dialog to pass the input value as a parameter to the called console application through the installer, using the nsDialog plugin
Function customPage
!insertmacro MUI_HEADER_TEXT "Something" "Tool"
nsDialogs::Create 1018
Pop $0
${NSD_CreateLabel} 0 0 100% 12u "enter text name"
Pop $0
Function customPage
!insertmacro MUI_HEADER_TEXT "Something" "Tool"
nsDialogs::Create 1018
Pop $0
${NSD_CreateLabel} 0 0 100% 12u "enter text name"
Pop $0
${NSD_CreateText} 0 12u 93% 12u
Pop $TextBox
nsDialogs::Show
FunctionEnd
nsDialogs::Show
FunctionEnd

Yes this is a common pattern. Just save the value when the user leaves the page so you can use it in a Section:
!include MUI2.nsh
!include nsDialogs.nsh
Page Custom MyPageCreate MyPageLeave
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE English
Var TextBox
Var CustomValue
Function .onInit
StrCpy $CustomValue "Hello World" ; Default value used by silent installer
FunctionEnd
Function MyPageCreate
!insertmacro MUI_HEADER_TEXT "Something" "Tool"
nsDialogs::Create 1018
Pop $0
${NSD_CreateLabel} 0 0 100% 12u "enter text name"
Pop $0
${NSD_CreateText} 0 12u 93% 12u "$CustomValue"
Pop $TextBox
nsDialogs::Show
FunctionEnd
Function MyPageLeave
${NSD_GetText} $TextBox $CustomValue
FunctionEnd
Section
Exec '"$sysdir\cmd.exe" /K echo.$CustomValue&pause&exit'
SectionEnd

Related

When I am clicking on "Finish" button or close button at the top right corner of the custom page, page is not closing using NSIS

I am facing a weird problem, I have added a new custom page (MyAbruptPage). When clicking on "Finish" button or close button at the top right corner of this custom page, page is not closing.
Scenario:
When Installing the software, if something went wrong and installation hasn't done properly it shows a MessageBox from INSTFILES page. Upon click on "OK" on the message box It is navigating to the "Abrupt" page. In the Abrupt Page it is showing "Finish" button, but when clicked on "Finish" the page is not closing instead it is showing a pop-up message (Are you sure you want to close?). Even when i clicked on close button in the top right corner also i am getting the same pop-up message. I don't want that message and the dialog should be closed when clicked on "Finish" button.
Observations:
I am thinking that the problem is due to the sequence of the pages. Because here i have used two custom pages (MyInfoPage, And MyAbruptPage).
If I replace the first page (MyInfoPage) with MyAbruptPage. Then I am not seeing any issue when clicking on "Finish" button in the Abrupt Page.
Below is my code snippet showing pages and its sequence:
Page Custom MyInfoPage ; First Custom page
!define MUI_PAGE_CUSTOMFUNCTION_SHOW WelShow
!insertmacro MUI_PAGE_WELCOME
!define MUI_PAGE_CUSTOMFUNCTION_SHOW MyLicenseShowCallback
!insertmacro MUI_PAGE_LICENSE
!define MUI_PAGE_CUSTOMFUNCTION_SHOW MyDirectoryShowCallback
!insertmacro MUI_PAGE_DIRECTORY
Page Custom MyAbruptPage ; Second Custom Page
!define MUI_PAGE_CUSTOMFUNCTION_SHOW AbruptShow
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE AbruptLeave
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
; Below is my code for the Abrupt page
Function AbruptShow
StrCpy $IsOnAbruptPage 1
GetDlgItem $0 $HWNDPARENT 3
ShowWindow $0 0
FunctionEnd
Function AbruptLeave
StrCpy $IsOnAbruptPage 0
FunctionEnd
Function MyAbruptPage
${IfThen} $IsOnAbruptPage == "" ${|} Abort ${|}
GetDlgItem $0 $HWNDPARENT 1
ShowWindow $0 0 ; Hide Next button
GetDlgItem $0 $HWNDPARENT 3
ShowWindow $0 0 ; Hide Back button
GetDlgItem $0 $HWNDPARENT 2
${NSD_SetText} $0 "Finish"
nsDialogs::Create 1044
Pop $0
${NSD_CreateLabel} 120u 10u 195u ${MUI_WELCOMEPAGE_TITLE_HEIGHT}u "$(AbruptTitle)"
Pop $AbruptPageTitle
;SetCtlColors $InterruptPageTitle "" "${MUI_BGCOLOR}"
CreateFont $AbruptPageFont "$(^Font)" "12" "700"
SendMessage $AbruptPageTitle ${WM_SETFONT} $AbruptPageFont 0
${NSD_CreateLabel} 120u ${MUI_WELCOMEPAGE_TEXT_TOP}u 195u 130u "$(AbruptText)"
Pop $AbruptPageText
Pop $0
nsDialogs::Show
${NSD_FreeImage} $ImageHandle
FunctionEnd
Below is the section and from here Message box is popping up and navigating to the Abrupt screen:
;!insertmacro MUI_LANGUAGE "English"
Section MySection
SetOutPath $INSTDIR
MessageBox MB_OK|MB_ICONEXCLAMATION "Installation hasn't done properly" IDOK
Call AbruptLeave
SendMessage $HWNDPARENT 0x408 -1 ""
Abort
SectionEnd
;Below is the code for the custompage "MyInfoPage"
Function MyInfoPage
${IfThen} $PageId == "" ${|} Abort ${|}
StrCpy $PageId 0
GetDlgItem $0 $HWNDPARENT 1
ShowWindow $0 0 ; Hide Next button
GetDlgItem $0 $HWNDPARENT 2
${NSD_SetText} $0 "Finish"
nsDialogs::Create 1044
Pop $0
${NSD_CreateLabel} 120u 10u 195u ${MUI_WELCOMEPAGE_TITLE_HEIGHT}u "$(InterruptTitle)"
Pop $InterruptPageTitle
${NSD_CreateLabel} 120u ${MUI_WELCOMEPAGE_TEXT_TOP}u 195u 130u "$(InterruptText)"
Pop $InterruptPageText
nsDialogs::Show
${NSD_FreeImage} $ImageHandle
FunctionEnd
Function WelShow
StrCpy $PageId 1
GetDlgItem $0 $HWNDPARENT 3
ShowWindow $0 0 ; Hide Back button
FunctionEnd
Function onAbort
${If} $PageId <> 0
${If} ${Cmd} ` MessageBox MB_YESNO "Are you sure you want to close?" IDYES `
SendMessage $HWNDPARENT 0x408 -$PageId ""
${EndIf}
Abort
${EndIf}
FunctionEnd
I have tried by changing the sequence of the pages and every time I am observing different behaviors in both the custom pages. Please help me how to resolve this issue? And the correct way to implement this functionality.
Thank you in advance...
Below is the complete code and it is compiling:
Var PageId
Var PrintBtn
Var Image
Var ImageHandle
Var MyInfoPageTitle
Var MyInfoPageFont
var MyInfoPageText
var IsOnAbruptPage
Var AbruptPageTitle
Var AbruptPageFont
var AbruptPageText
!define MUI_CUSTOMFUNCTION_ABORT onAbort
!include "MUI2.nsh"
!include x64.nsh
Name "MyApp"
OutFile "MyApp.exe"
InstallDir "$PROGRAMFILES32\MyApp"
InstallDirRegKey HKLM "Software\MyApp" "Install_Dir"
RequestExecutionLevel admin
!include LogicLib.nsh
;--------------------------------
Page Custom MyInfoPage
!define MUI_PAGE_CUSTOMFUNCTION_SHOW WelShow
!define MUI_TEXT_WELCOME_INFO_TITLE $(welcometitle)
!define MUI_TEXT_WELCOME_INFO_TEXT $(welcometext)
!insertmacro MUI_PAGE_WELCOME
!define MUI_PAGE_CUSTOMFUNCTION_SHOW MyLicenseShowCallback
!define MUI_LICENSEPAGE_CHECKBOX ""
!define MUI_INNERTEXT_LICENSE_BOTTOM ""
!define MUI_INNERTEXT_LICENSE_TOP ""
!define MUI_INNERTEXT_LICENSE_BOTTOM_CHECKBOX ""
!define MUI_TEXT_LICENSE_TITLE $(licensetitle)
!define MUI_TEXT_LICENSE_SUBTITLE $(licensesubtitle)
!define MUI_LICENSEPAGE_CHECKBOX_TEXT $(licensecheckboxtext)
!insertmacro MUI_PAGE_LICENSE "C:\Program Files (x86)\NSIS\Docs\Modern UI 2\license.txt"
!define MUI_PAGE_CUSTOMFUNCTION_SHOW MyDirectoryShowCallback
!define MUI_DIRECTORYPAGE_TEXT_TOP $(mydirtoptext)
!define MUI_TEXT_DIRECTORY_TITLE $(mydirtitle)
!define MUI_TEXT_DIRECTORY_SUBTITLE $(mydirsubtitle)
!insertmacro MUI_PAGE_DIRECTORY
Page Custom MyAbruptPage
!define MUI_PAGE_CUSTOMFUNCTION_SHOW AbruptShow
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE AbruptLeave
!insertmacro MUI_PAGE_INSTFILES
!define MUI_TEXT_FINISH_INFO_TITLE $(Finishpagetitle)
!define MUI_TEXT_FINISH_INFO_TEXT $(Finishpagesubtitle)
!define MUI_FINISHPAGE_RUN ""
!define MUI_FINISHPAGE_RUN_TEXT $(FinishonlineReg)
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_UNPAGE_INSTFILES
;--------------------------------
!insertmacro MUI_LANGUAGE "English"
Function .onInit
InitPluginsDir
FunctionEnd
Function MyDirectoryShowCallback
StrCpy $PageId 3
GetDlgItem $0 $hwndparent 1 ;
SendMessage $0 ${WM_SETTEXT} 0 `STR:$(^NextBtn)`
FunctionEnd
Function MyLicenseShowCallback
StrCpy $PageId 2
GetDlgItem $0 $hwndparent 2
System::Call *(i,i,i,i)p.r1
System::Call 'USER32::GetWindowRect(pr0,pr1)'
System::Call *$1(i.r2,i.r3,i.r4,i.r5)
IntOp $5 $5 - $3 ;height
IntOp $4 $4 - $2 ;width
System::Call 'USER32::ScreenToClient(p$hwndparent,pr1)'
System::Call *$1(i.r2,i.r3)
System::Free $1
IntOp $2 $4 / 5
System::Call 'USER32::CreateWindowEx(i 0,t "Button",t "Print",i ${WS_CHILD}|${WS_VISIBLE}|${WS_TABSTOP},i r2,i r3,i r4,i r5,p $hwndparent,p 0x666,p 0,p 0)p.r0'
StrCpy $PrintBtn $0
SendMessage $hwndparent ${WM_GETFONT} 0 0 $1
SendMessage $0 ${WM_SETFONT} $1 1
ButtonEvent::AddEventHandler 0x666 $0
FunctionEnd
Function WelShow
StrCpy $PageId 1
GetDlgItem $0 $HWNDPARENT 3
ShowWindow $0 0
FunctionEnd
Function MyInfoPage
${IfThen} $PageId == "" ${|} Abort ${|}
StrCpy $PageId 0
GetDlgItem $0 $HWNDPARENT 1
ShowWindow $0 0
GetDlgItem $0 $HWNDPARENT 2
${NSD_SetText} $0 "Finish"
nsDialogs::Create 1044
Pop $0
!define MUI_WELCOMEPAGE_TITLE_HEIGHT 38
!define /math MUI_WELCOMEPAGE_TEXT_TOP 17 + ${MUI_WELCOMEPAGE_TITLE_HEIGHT}
${NSD_CreateLabel} 120u 10u 195u 28u "Setup Wizard was interrupted"
Pop $MyInfoPageTitle
CreateFont $MyInfoPageFont "$(^Font)" "12" "700"
SendMessage $MyInfoPageTitle ${WM_SETFONT} $MyInfoPageFont 0
${NSD_CreateLabel} 120u ${MUI_WELCOMEPAGE_TEXT_TOP}u 195u 130u "Setup Wizard was interrupted"
Pop $MyInfoPageText
Pop $0
nsDialogs::Show
${NSD_FreeImage} $ImageHandle
FunctionEnd
Function onAbort
${If} $PageId <> 0
${If} ${Cmd} ` MessageBox MB_YESNO "Are you sure you want to CANCEL" IDYES `
SendMessage $HWNDPARENT 0x408 -$PageId ""
${EndIf}
Abort
${EndIf}
FunctionEnd
Function AbruptShow
StrCpy $IsOnAbruptPage 1
GetDlgItem $0 $HWNDPARENT 3
ShowWindow $0 0
FunctionEnd
Function AbruptLeave
StrCpy $IsOnAbruptPage 0
FunctionEnd
Function MyAbruptPage
${IfThen} $IsOnAbruptPage == "" ${|} Abort ${|}
GetDlgItem $0 $HWNDPARENT 1
ShowWindow $0 0
GetDlgItem $0 $HWNDPARENT 3
ShowWindow $0 0
GetDlgItem $0 $HWNDPARENT 2
${NSD_SetText} $0 "Finish"
nsDialogs::Create 1044
Pop $0
${NSD_CreateLabel} 120u 10u 195u ${MUI_WELCOMEPAGE_TITLE_HEIGHT}u "Setup Wizard ended prematurely"
Pop $AbruptPageTitle
CreateFont $AbruptPageFont "$(^Font)" "12" "700"
SendMessage $AbruptPageTitle ${WM_SETFONT} $AbruptPageFont 0
${NSD_CreateLabel} 120u ${MUI_WELCOMEPAGE_TEXT_TOP}u 195u 130u "Setup Wizard ended prematurely"
Pop $AbruptPageText
Pop $0
nsDialogs::Show
${NSD_FreeImage} $ImageHandle
FunctionEnd
SectionIn RO
SetOutPath $INSTDIR
MessageBox MB_OK|MB_ICONEXCLAMATION "There is an installation failure. Aborting the installation process" IDOK
Call AbruptLeave
SendMessage $HWNDPARENT 0x408 -1 ""
Abort
SectionEnd
Section "Uninstall"
SectionEnd
The MessageBox you see is coming from your own code in your onAbort function!
The fact that you are doing
!define MUI_PAGE_CUSTOMFUNCTION_SHOW AbruptShow
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE AbruptLeave
shows that you have don't understand how custom pages work.
To make your special abort page quit without further action you simple have to do nothing in your onAbort function when you are on that page:
Var PageId
var IsOnAbruptPage
!define MUI_CUSTOMFUNCTION_ABORT onAbort
!include "MUI2.nsh"
!include LogicLib.nsh
Name "MyApp"
#OutFile "MyApp.exe"
InstallDir "$temp\MyApp"
#RequestExecutionLevel admin
OutFile Test.exe
RequestExecutionLevel user
;--------------------------------
Page Custom MyInfoPage
!define MUI_PAGE_CUSTOMFUNCTION_SHOW WelShow
!insertmacro MUI_PAGE_WELCOME
!define MUI_PAGE_CUSTOMFUNCTION_SHOW MyLicenseShowCallback
!insertmacro MUI_PAGE_LICENSE "${__FILE__}"
!define MUI_PAGE_CUSTOMFUNCTION_SHOW MyDirectoryShowCallback
!insertmacro MUI_PAGE_DIRECTORY
Page Custom MyAbruptPage
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
;--------------------------------
!insertmacro MUI_LANGUAGE "English"
Function MyDirectoryShowCallback
StrCpy $PageId 3
GetDlgItem $0 $hwndparent 1 ;
SendMessage $0 ${WM_SETTEXT} 0 `STR:$(^NextBtn)`
FunctionEnd
Function MyLicenseShowCallback
StrCpy $PageId 2
FunctionEnd
Function WelShow
StrCpy $PageId 1
GetDlgItem $0 $HWNDPARENT 3
ShowWindow $0 0
FunctionEnd
Function MyInfoPage
${IfThen} $PageId == "" ${|} Abort ${|}
StrCpy $PageId 0
GetDlgItem $0 $HWNDPARENT 1
ShowWindow $0 0
GetDlgItem $0 $HWNDPARENT 2
${NSD_SetText} $0 "Finish"
nsDialogs::Create 1044
Pop $0
${NSD_CreateLabel} 120u 10u 195u 28u "Setup Wizard was interrupted"
Pop $0
nsDialogs::Show
FunctionEnd
Function onAbort
${If} $IsOnAbruptPage <> 0 ; If on aborted page
Return ; allow simple exit
${EndIf}
${If} $PageId <> 0
${If} ${Cmd} ` MessageBox MB_YESNO "Are you sure you want to CANCEL" IDYES `
SendMessage $HWNDPARENT 0x408 -$PageId ""
${EndIf}
Abort
${EndIf}
FunctionEnd
Function GoToAbruptPage
StrCpy $IsOnAbruptPage 1
SendMessage $HWNDPARENT 0x408 -1 ""
Abort
FunctionEnd
Function MyAbruptPage
${IfThen} $IsOnAbruptPage == "" ${|} Abort ${|}
GetDlgItem $0 $HWNDPARENT 1
ShowWindow $0 0
GetDlgItem $0 $HWNDPARENT 3
ShowWindow $0 0
GetDlgItem $0 $HWNDPARENT 2
${NSD_SetText} $0 "Finish"
nsDialogs::Create 1044
Pop $0
${NSD_CreateLabel} 120u 10u 195u 20u "Setup Wizard ended prematurely"
Pop $0
${NSD_CreateLabel} 120u 120u 195u 20u "Setup Wizard ended prematurely"
Pop $0
nsDialogs::Show
FunctionEnd
;--------------------------------
Section "$(^Name) (required)"
SectionIn RO
SetOutPath $INSTDIR
MessageBox MB_OK|MB_ICONEXCLAMATION "There is an installation failure. Aborting the installation process" IDOK
Call GoToAbruptPage
SectionEnd

${NSD_GetText} always returns the empty string

Per the manual, I should be able to get the text of a text control with code like this:
${NSD_GetText} $TextBox $0
MessageBox MB_OK "You typed:$\n$\n$0"
I always get the empty string out of this call. In the code below, the text box shows "correct" but the details always show Contents:; if I comment the call to ${NSD_GetText}, I get Contents: wrong.
!include nsDialogs.nsh
!include LogicLib.nsh
Var Dialog
Var TextBox
Page custom nsDialogsPage nsDialogsPageLeave
Page instfiles
Function nsDialogsPage
StrCpy $0 "wrong"
nsDialogs::Create 1018
Pop $Dialog
${If} $Dialog == error
Abort
${EndIf}
${NSD_CreateText} 0 12u 93% 12u "correct"
Pop $TextBox
nsDialogs::Show
FunctionEnd
Function nsDialogsPageLeave
FunctionEnd
Section
${NSD_GetText} $TextBox $0
DetailPrint "Contents: $0"
SectionEnd
So I thought maybe the control didn't exist when I was trying to print its contents, and tried updating the text as it was typed into the control; that didn't help. It's implausible that NSIS is broken in this way, so what am I doing wrong?
!include nsDialogs.nsh
!include LogicLib.nsh
Var Dialog
Var TextBox
Var Text
Page custom nsDialogsPage nsDialogsPageLeave
Page instfiles
Function nsDialogsPage
StrCpy $0 "wrong"
nsDialogs::Create 1018
Pop $Dialog
${If} $Dialog == error
Abort
${EndIf}
${NSD_CreateText} 0 12u 93% 12u "correct"
Pop $TextBox
${NSD_OnChange} $TextBox UpdateText
nsDialogs::Show
FunctionEnd
Function nsDialogsPageLeave
FunctionEnd
Function UpdateText
${NSD_GetText} $TextBox $Text
FunctionEnd
Section
DetailPrint "Contents: $Text"
SectionEnd
You are correct, the control does not exist in the Section so you have to get the contents while you are on the custom page.
Your second example should work correctly if the user changes the text but not if they don't because the change event would not fire.
You normally just read the content in the page leave callback:
Var Dialog
Var TextBox
Var Text
!include LogicLib.nsh
!include nsDialogs.nsh
Page custom nsDialogsPage nsDialogsPageLeave
Page instfiles
Function nsDialogsPage
nsDialogs::Create 1018
Pop $Dialog
${If} $Dialog == error
Abort
${EndIf}
${NSD_CreateText} 0 12u 93% 12u "correct"
Pop $TextBox
nsDialogs::Show
FunctionEnd
Function nsDialogsPageLeave
${NSD_GetText} $TextBox $Text
FunctionEnd
Section
DetailPrint "Contents: $Text"
SectionEnd

NSIS: How do I allow multiple user choice screens that select the sections to install?

I'm attempting to create an installer that asks the user a series of questions to decide which components to install. Each choice (potentially) influences the available options in later choices (else I would just do a normal components page—I don't want to give the user invalid options).
How do I accomplish such a thing? If I just use the components page, all the options are shown, some combinations of which are completely invalid. I don't want to let the user select those. Is it possible to leave out the components page?
Here's a minimal working example of what I'm trying. (Sorry it's long, I couldn't really simplify the dialog code.)
!include nsDialogs.nsh
!include Sections.nsh
Name "mwe"
OutFile "mwe.exe"
InstallDir C:\mwe
Var hwnd
Var Level1Opt
Page custom SelectLevel1Opt ProcessLevel1
Function SelectLevel1Opt
nsDialogs::Create 1018
pop $hwnd
${NSD_CreateLabel} 0 0 100% 12u "Please select level 1 option"
Pop $hwnd
${NSD_CreateRadioButton} 10% 12u 100% 12u "Level 1 A"
Pop $hwnd
nsDialogs::SetUserData $hwnd "Level 1 A"
${NSD_OnClick} $hwnd SetLevel1
${NSD_CreateRadioButton} 10% 24u 100% 12u "Level 1 B"
Pop $hwnd
nsDialogs::SetUserData $hwnd "Level 1 B"
${NSD_OnClick} $hwnd SetLevel1
nsDialogs::Show
FunctionEnd
Function SetLevel1
Pop $hwnd
nsDialogs::GetUserData $hwnd
Pop $Level1Opt
MessageBox MB_OK "Selected: $Level1Opt"
FunctionEnd
Function ProcessLevel1
${If} $Level1Opt == "Level 1 A"
!insertmacro SelectSection Level1A
${ElseIf} $Level1Opt == "Level 1 B"
!insertmacro SelectSection Level1B
${EndIf}
FunctionEnd
Page directory
Page instfiles
Section ""
MessageBox MB_OK "Common Install"
SectionEnd
Section /o "" Level1A
MessageBox MB_OK "Level 1 A"
SectionEnd
Section /o "" Level1B
MessageBox MB_OK "Level 1 B"
SectionEnd
No matter what I choose, neither Level1A nor Level1B sections get run. The selection from the dialog is correctly detected in the handler and the post function. However, selecting the sections isn't causing them to run. Even if I add a components page, neither of them is selected.
I looked in Selection.nsh, and the example it refers to (one-section.nsi) doesn't really do what I want, because it uses the components page. (I also don't understand quite how it works.)
What am I doing wrong? In what way am I misunderstanding the way NSIS is supposed to work?
As idleberg says, the correct syntax is !insertmacro SelectSection ${Level1A} but you get a warning because the section id is not defined until after its Section instruction in your .nsi. You need to move the functions that use ${Level1A} below the sections in your source code:
!include nsDialogs.nsh
!include Sections.nsh
Page custom SelectLevel1Opt ProcessLevel1
Page instfiles
Section ""
MessageBox MB_OK "Common Install"
SectionEnd
Section /o "a" Level1A
MessageBox MB_OK "Level 1 A"
SectionEnd
Section /o "b" Level1B
MessageBox MB_OK "Level 1 B"
SectionEnd
Var hInnerDialog
Var hL1A
Var hL1B
Function SelectLevel1Opt
nsDialogs::Create 1018
pop $hInnerDialog
${NSD_CreateLabel} 0 0 100% 12u "Please select level 1 option"
Pop $0
${NSD_CreateRadioButton} 10% 12u 100% 12u "Level 1 A"
Pop $hL1A
nsDialogs::SetUserData $hL1A ${Level1A} ; Only used by the generic function
${NSD_CreateRadioButton} 10% 24u 100% 12u "Level 1 B"
Pop $hL1B
nsDialogs::SetUserData $hL1B ${Level1B} ; Only used by the generic function
nsDialogs::Show
FunctionEnd
Function ProcessLevel1
${NSD_GetState} $hL1A $1
${If} $1 <> ${BST_UNCHECKED}
!insertmacro SelectSection ${Level1A}
!insertmacro UnselectSection ${Level1B}
${Else}
!insertmacro SelectSection ${Level1B}
!insertmacro UnselectSection ${Level1A}
${EndIf}
FunctionEnd
The ProcessLevel1 function can also be implemented as a loop if there are many radio buttons:
Function ProcessLevel1
StrCpy $0 ""
loop:
FindWindow $0 "${__NSD_RadioButton_CLASS}" "" $hInnerDialog $0
System::Call "USER32::GetWindowLong(p$0,i${GWL_STYLE})i.r1"
IntOp $1 $1 & ${BS_AUTORADIOBUTTON}
${If} $1 = ${BS_AUTORADIOBUTTON} ; Is it a auto radio button?
nsDialogs::GetUserData $0 ; Get the section id
Pop $2
${NSD_GetState} $0 $1
${If} $1 <> ${BST_UNCHECKED}
!insertmacro SelectSection $2
${Else}
!insertmacro UnselectSection $2
${EndIf}
${EndIf}
IntCmp $0 0 "" loop loop
FunctionEnd

NSIS selected options odd behaviour

I am not an NSIS pro but I get by! I am setting up a new installer for a new piece of software we are building, I have configured the installer up with 2 potential paths, install or upgrade.
The idea is that when a user selects any of the upgrade options all options that may be selected for install are deselected and vice versa.
What is happening for me at the moment is when a user for example, selects one option under the install path, all options for that path are being selected and are not deselectable unless you select an option under the upgrade path, after which interestingly, selection of options then appears to behave as wanted?
Here is my code, what obvious thing am I missing here??
;--------------------------------
;Includes
;--------------------------------
!include "MUI2.nsh"
!include "nsDialogs.nsh"
!include "LogicLib.nsh"
!include "Sections.nsh"
!include "oledb.nsh"
!include "WinMessages.nsh"
!include "ReplaceInFile.nsh"
;--------------------------------
;General
;--------------------------------
;Name and file
Name "App"
OutFile "AppName ${Version}.exe"
;Default installation folder
InstallDir "C:\appFolder"
;Request application privileges for Windows Vista +
RequestExecutionLevel admin
;--------------------------------
;Interface Settings
;--------------------------------
!define MUI_ABORTWARNING
Var Dialog
Var SQLServer
Var SQLUsername
Var SQLPassword
Var SDatabase
Var CoreDatabase
Var UIDatabase
ShowInstDetails "show"
;--------------------------------
;Pages
;--------------------------------
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_LICENSE "LIC.rtf"
!insertmacro MUI_PAGE_COMPONENTS
Page Custom SQLConnectionDetails SQLConnectionDetailsPageLeave
Page Custom DatabaseDetails DatabaseDetailsLeave
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
;--------------------------------
;onInit
;--------------------------------
Function .onInit
InitPluginsDir
StrCpy $1 "SecMain"
FunctionEnd
;--------------------------------
;Page functions
;--------------------------------
Function SQLConnectionDetails
!insertmacro MUI_HEADER_TEXT $(SQLServerDetailsTitle) $(SQLServerDetailsSubTitle)
nsDialogs::Create 1018
pop $Dialog
${If} $Dialog == error
Abort
${EndIf}
;x, y, width, height and text
${NSD_CreateLabel} 0 0 100% 10u "Enter the name of the SQL server that AppName is to be installed on"
pop $1
${NSD_CreateText} 0 10u 150u 12u ""
pop $SQLServer
${NSD_CreateLabel} 0 25u 100% 10u "Enter the database name"
pop $2
${NSD_CreateText} 0 35u 100u 12u ""
pop $SDatabase
${NSD_CreateLabel} 0 50u 100% 10u "Enter SQL username"
pop $3
${NSD_CreateText}} 0 60u 100u 12u ""
pop $SQLUsername
${NSD_CreateLabel} 0 75u 100% 10u "Enter SQL password"
pop $4
${NSD_CreatePassword} 0 85u 100u 12u ""
pop $SQLPassword
nsDialogs::Show
FunctionEnd
Function SQLConnectionDetailsPageLeave
${NSD_GetText} $SQLServer $SQLServer
${NSD_GetText} $SQLUsername $SQLUsername
${NSD_GetText} $SQLPassword $SQLPassword
${NSD_GetText} $SDatabase $SDatabase
messagebox MB_OK "We will now attempt to connect to the database server."
MSSQL_OLEDB::SQL_Logon "$SQLServer" "$SQLUsername" "$SQLPassword"
pop $0
${If} $0 = 0
;messagebox MB_OK "Connection sucessful"
${Else}
messagebox MB_OK "I encountered an issue whilst trying to connect to the database. Please check your details and try again."
abort
${EndIf}
MSSQL_OLEDB::SQL_Logout
FunctionEnd
Function DatabaseDetails
!insertmacro MUI_HEADER_TEXT $(DatabaseNamesTitle) $(DatabaseNamesSubTitle)
nsDialogs::Create 1018
pop $Dialog
${If} $Dialog == error
Abort
${EndIf}
;x, y, width, height and text
${NSD_CreateLabel} 0 0 100% 10u "Enter the name to be given to the Core database"
pop $1
${NSD_CreateText} 0 10u 150u 12u ""
pop $CoreDatabase
${NSD_CreateLabel} 0 25u 100% 10u "Enter the name to be given to the UI database"
pop $2
${NSD_CreateText} 0 35u 100u 12u ""
pop $UIDatabase
nsDialogs::Show
FunctionEnd
Function DatabaseDetailsLeave
${NSD_GetText} $CoreDatabase $CoreDatabase
${NSD_GetText} $UIDatabase $UIDatabase
FunctionEnd
;--------------------------------
;Languages
;--------------------------------
!insertmacro MUI_LANGUAGE "English"
;--------------------------------
;Installer Sections
;--------------------------------
InstType "New Install"
InstType "New Install - Full"
InstType "Upgrade"
;------------
;New Install
;------------
SectionGroup /e "!Install" SecGroupNewInstall
Section "Main App" SecMain
SectionEnd
Section /o "App1" SecApp1
SectionEnd
Section /o "App2" SecApp2
SectionEnd
Section /o "App3" SecApp3
SectionEnd
Section /o "App4" SecApp4
SectionEnd
SectionGroup /e "Optional" SubSecGroupNewInstall
Section /o "App5" SecApp5
SectionEnd
Section /o "App6" SecApp6
SectionEnd
Section /o "App7" SecApp7
SectionEnd
SectionGroupEnd
SectionGroupEnd
SectionGroup /e "!Upgrade" SecGroupUpgrade
Section /o "Upgrade Main App" SecMainUpgrade
SectionEnd
SectionGroupEnd
;--------------------------------
;onSelChange
;--------------------------------
Function .onSelChange
!insertmacro StartRadioButtons $1
!insertmacro RadioButton ${SecMain}
!insertmacro RadioButton ${SecMainUpgrade}
!insertmacro EndRadioButtons
!insertmacro SectionFlagIsSet ${SecMain} ${SF_SELECTED} InstisSel InstnotSel
InstnotSel:
!insertmacro UnselectSection ${SecGroupNewInstall}
InstisSel:
!insertmacro SectionFlagIsSet ${SecMainUpgrade} ${SF_SELECTED} UpgisSel UpgnotSel
UpgnotSel:
!insertmacro UnselectSection ${SecGroupUpgrade}
UpgisSel:
FunctionEnd
I have answered my own question!!
The issue was down to both timing, and an issue with my onInit function. I have moved my onInit function below my section groups and corrected the StrCpy to now include the correct section reference, see corrected code below:
;--------------------------------
;Includes
;--------------------------------
!include "MUI2.nsh"
!include "nsDialogs.nsh"
!include "LogicLib.nsh"
!include "Sections.nsh"
!include "oledb.nsh"
!include "WinMessages.nsh"
!include "ReplaceInFile.nsh"
;--------------------------------
;General
;--------------------------------
;Name and file
Name "App"
OutFile "AppName ${Version}.exe"
;Default installation folder
InstallDir "C:\appFolder"
;Request application privileges for Windows Vista +
RequestExecutionLevel admin
;--------------------------------
;Interface Settings
;--------------------------------
!define MUI_ABORTWARNING
Var Dialog
Var SQLServer
Var SQLUsername
Var SQLPassword
Var SDatabase
Var CoreDatabase
Var UIDatabase
ShowInstDetails "show"
;--------------------------------
;Pages
;--------------------------------
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_LICENSE "LIC.rtf"
!insertmacro MUI_PAGE_COMPONENTS
Page Custom SQLConnectionDetails SQLConnectionDetailsPageLeave
Page Custom DatabaseDetails DatabaseDetailsLeave
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
;--------------------------------
;Page functions
;--------------------------------
Function SQLConnectionDetails
!insertmacro MUI_HEADER_TEXT $(SQLServerDetailsTitle) $(SQLServerDetailsSubTitle)
nsDialogs::Create 1018
pop $Dialog
${If} $Dialog == error
Abort
${EndIf}
;x, y, width, height and text
${NSD_CreateLabel} 0 0 100% 10u "Enter the name of the SQL server that AppName is to be installed on"
pop $1
${NSD_CreateText} 0 10u 150u 12u ""
pop $SQLServer
${NSD_CreateLabel} 0 25u 100% 10u "Enter the database name"
pop $2
${NSD_CreateText} 0 35u 100u 12u ""
pop $SDatabase
${NSD_CreateLabel} 0 50u 100% 10u "Enter SQL username"
pop $3
${NSD_CreateText}} 0 60u 100u 12u ""
pop $SQLUsername
${NSD_CreateLabel} 0 75u 100% 10u "Enter SQL password"
pop $4
${NSD_CreatePassword} 0 85u 100u 12u ""
pop $SQLPassword
nsDialogs::Show
FunctionEnd
Function SQLConnectionDetailsPageLeave
${NSD_GetText} $SQLServer $SQLServer
${NSD_GetText} $SQLUsername $SQLUsername
${NSD_GetText} $SQLPassword $SQLPassword
${NSD_GetText} $SDatabase $SDatabase
messagebox MB_OK "We will now attempt to connect to the database server."
MSSQL_OLEDB::SQL_Logon "$SQLServer" "$SQLUsername" "$SQLPassword"
pop $0
${If} $0 = 0
;messagebox MB_OK "Connection sucessful"
${Else}
messagebox MB_OK "I encountered an issue whilst trying to connect to the database. Please check your details and try again."
abort
${EndIf}
MSSQL_OLEDB::SQL_Logout
FunctionEnd
Function DatabaseDetails
!insertmacro MUI_HEADER_TEXT $(DatabaseNamesTitle) $(DatabaseNamesSubTitle)
nsDialogs::Create 1018
pop $Dialog
${If} $Dialog == error
Abort
${EndIf}
;x, y, width, height and text
${NSD_CreateLabel} 0 0 100% 10u "Enter the name to be given to the Core database"
pop $1
${NSD_CreateText} 0 10u 150u 12u ""
pop $CoreDatabase
${NSD_CreateLabel} 0 25u 100% 10u "Enter the name to be given to the UI database"
pop $2
${NSD_CreateText} 0 35u 100u 12u ""
pop $UIDatabase
nsDialogs::Show
FunctionEnd
Function DatabaseDetailsLeave
${NSD_GetText} $CoreDatabase $CoreDatabase
${NSD_GetText} $UIDatabase $UIDatabase
FunctionEnd
;--------------------------------
;Languages
;--------------------------------
!insertmacro MUI_LANGUAGE "English"
;--------------------------------
;Installer Sections
;--------------------------------
InstType "New Install"
InstType "New Install - Full"
InstType "Upgrade"
;------------
;New Install
;------------
SectionGroup /e "!Install" SecGroupNewInstall
Section "Main App" SecMain
SectionEnd
Section /o "App1" SecApp1
SectionEnd
Section /o "App2" SecApp2
SectionEnd
Section /o "App3" SecApp3
SectionEnd
Section /o "App4" SecApp4
SectionEnd
SectionGroup /e "Optional" SubSecGroupNewInstall
Section /o "App5" SecApp5
SectionEnd
Section /o "App6" SecApp6
SectionEnd
Section /o "App7" SecApp7
SectionEnd
SectionGroupEnd
SectionGroupEnd
SectionGroup /e "!Upgrade" SecGroupUpgrade
Section /o "Upgrade Main App" SecMainUpgrade
SectionEnd
SectionGroupEnd
;--------------------------------
;onInit
;--------------------------------
Function .onInit
InitPluginsDir
StrCpy $1 ${SecMain}
FunctionEnd
;--------------------------------
;onSelChange
;--------------------------------
Function .onSelChange
!insertmacro StartRadioButtons $1
!insertmacro RadioButton ${SecMain}
!insertmacro RadioButton ${SecMainUpgrade}
!insertmacro EndRadioButtons
!insertmacro SectionFlagIsSet ${SecMain} ${SF_SELECTED} InstisSel InstnotSel
InstnotSel:
!insertmacro UnselectSection ${SecGroupNewInstall}
InstisSel:
!insertmacro SectionFlagIsSet ${SecMainUpgrade} ${SF_SELECTED} UpgisSel UpgnotSel
UpgnotSel:
!insertmacro UnselectSection ${SecGroupUpgrade}
UpgisSel:
FunctionEnd

How to use REMOVE or REPAIR feature in NSIS script?

I have used nsis script for creating installer.When i run my installer second time with same name,REPAIR and REMOVE should be check and do the corresponding operation.I have find out my application already installed or not using following codes,
Function checkinstall
ReadRegStr $R0 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\My app" "UninstallString"
IfFileExists $R0 +1 NotInstalled
call nsDialogpage
NotInstalled:
FunctionEnd
Function nsDialogpage
nsDialogs::Create 1018
Pop $Dialog
${If} $Dialog == error
Abort
${EndIf}
${NSD_CreateRadioButton} 0 5u 100% 10u "Repair"
Pop $hwnd
${NSD_AddStyle} $hwnd ${WS_GROUP}
${NSD_OnClick} $hwnd ???
${NSD_CreateRadioButton} 0 25u 100% 56u "Remove"
Pop $hwnd
${NSD_OnClick} $hwnd ???
nsDialogs::Show
If the user select repair button it should overwrites existing installation path else uninstall existing installed and continue with new one.what am i need do to replace the (???) of the above code
page custom checkinstall
!insertmacro MUI_PAGE_DIRECTORY
My next page is Directory selection.so i need to call this page? How to achieve this?
1.How can i call un installer function if the user selects remove button?
Function un.Init, section /o -un.Main UNSEC000,section -un.post UNSE001
these are the un installer funtions.How can i call these functions? i have tried call method but it did not work.
You need to specify a callback function, like in the nsDialogs documentation, look for the nsDialogsPageLeave function in this example:
!include nsDialogs.nsh
!include LogicLib.nsh
Name nsDialogs
OutFile nsDialogs.exe
XPStyle on
Var Dialog
Var Label
Var Text
Page custom nsDialogsPage nsDialogsPageLeave
Page instfiles
Function nsDialogsPage
nsDialogs::Create 1018
Pop $Dialog
${If} $Dialog == error
Abort
${EndIf}
${NSD_CreateLabel} 0 0 100% 12u "Hello, welcome to nsDialogs!"
Pop $Label
${NSD_CreateText} 0 13u 100% -13u "Type something here..."
Pop $Text
${NSD_OnChange} $Text nsDialogsPageTextChange
nsDialogs::Show
FunctionEnd
Function nsDialogsPageLeave
${NSD_GetText} $Text $0
MessageBox MB_OK "You typed:$\n$\n$0"
FunctionEnd
Function nsDialogsPageTextChange
Pop $1 # $1 == $ Text
${NSD_GetText} $Text $0
${If} $0 == "hello"
MessageBox MB_OK "right back at ya!"
${EndIf}
FunctionEnd
Section
DetailPrint "hello world"
SectionEnd

Resources