How to make NSIS section mandatory depending on condition - nsis

I have a section in my installer I am attempting to make mandatory depending on some condition (the presence of a registry value) but I am not getting the behaviour I am expecting.
I am attempting to use the following command to make the section ticked and locked:
SectionIn RO
I also have a registry value that definitely exists but both of the following code chunks cause the section to be locked and ticked...
ClearErrors
ReadRegStr $0 HKCU "Software\Test" "TestValue"
${If} ${Errors}
SectionIn RO ; registry key not found
${Else}
; do nothing
${EndIf}
ClearErrors
ReadRegStr $0 HKCU "Software\Test" "TestValue"
${If} ${Errors}
; do nothing
${Else}
SectionIn RO ; registry key was found
${EndIf}
So it seems like either both control paths are being executed or the SectionIn command is trancending the if logic.
I can't seem to find much documentation on this particular command but I am a bit stumped. Any ideas?

SectionIn is an attribute and cannot be changed at run-time.
You should use the section helper macros in .onInit:
Section "blah" S_1
SectionEnd
!include LogicLib.nsh
!include Sections.nsh
Function .onInit
...
${If} ${Errors}
!insertmacro SetSectionFlag ${S_1} ${SF_RO}
${EndIf}
FunctionEnd

Related

NSIS nsi script error :- !insertmacro: macro named "SECTION_BEGIN" not found

In nsi script with MUI2.nsh
Code:-
!macro SECTION_BEGIN
Section ""
Call zip2exe.SetOutPath
!macroend
!macro SECTION_END
SectionEnd
!macroend
But If I want to define two or more section then in that case how to incorporate SECTION_BEGIN part?
Section "Main Component" MainCom
#SectionIn RO # Just means if in component mode this is locked
Call zip2exe.SetOutPath
;Store installation folder in registry
WriteRegStr HKLM "Software\${ZIP2EXE_NAME}" "" $INSTDIR
;Registry information for add/remove programs
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${ZIP2EXE_NAME}" "DisplayName" "${ZIP2EXE_NAME}"
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${ZIP2EXE_NAME}" "NoRepair" 1
;Create optional start menu shortcut for uninstaller and Main component
;Create uninstaller
WriteUninstaller "${ZIP2EXE_NAME}_uninstaller.exe"
#!macroend
#!macro SECTION_END
SectionEnd
#!macroend
;--------------------------------
;Uninstaller Section
Section "Uninstall"
;Delete the appdata directory + files
RMDir /r "${INSTDIR_DATA}\*.*"
RMDir "${INSTDIR_DATA}"
;Delete Start Menu Shortcuts
Delete "$SMPROGRAMS\${ZIP2EXE_NAME}\*.*"
RmDir "$SMPROGRAMS\${ZIP2EXE_NAME}"
SectionEnd
#!macro SECTION_END
If we omit SECTION_BEGIN part then error comes. If we mention SECTION_BEGIN in both sections then also error comes.
What will be the solution to this problem?
If actually want to use Zip2Exe then you can modify parts of NSIS\Contrib\zip2exe\Base.nsh from
!macro SECTION_END
SectionEnd
!macroend
to something like this
!macro SECTION_END
SectionEnd
!if /FileExists "c:\mycustomzip2exefiles\mycustomsections.nsh"
!include "c:\mycustomzip2exefiles\mycustomsections.nsh"
!endif
!macroend
You can then put whatever code you want in c:\mycustomzip2exefiles\mycustomsections.nsh:
Section "My other section"
SetOutPath $InstDir
File "anotherfile.txt"
SectionEnd
However, Zip2Exe is mainly something you use to create simple self-extracting executables, you should not use it to create full installers.
When you create a real installer you don't use Zip2Exe, you use MakeNSIS and there is no such thing as a SECTION_BEGIN macro, you just add as many sections as you want to your .NSI file.
Example2.nsi contains a basic installer/uninstaller.

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 I can do a section in other section?

I want to make a section in other section, how I can do this thing ?
Section /o "Control System Toolbox"
Section "Communications Toolbox"
SectionIn RO
FileWrite $9 "product=Control System Toolbox$\r$\n"
AddSize 0
SectionEnd
FileWrite $9 "product=Control System Toolbox$\r$\n"
AddSize 0
SectionEnd
You can organize sections in section groups. The groups cannot execute code but you can put hidden sections in them:
Page Components
Page InstFiles
SectionGroup /e "Main Component"
Section "" SID_MAIN
DetailPrint "Installing Main Component..."
SectionEnd
Section /o "Bonus feature" SID_BONUS
DetailPrint "Installing bonus Component..."
SectionEnd
SectionGroupEnd
!include Sections.nsh
!include LogicLib.nsh
Function .OnSelChange
${If} ${SectionIsSelected} ${SID_BONUS}
!insertmacro SelectSection ${SID_MAIN} ; The main component is required when installing the bonus component
${EndIf}
FunctionEnd

Changing the selection in the components page of NSIS based on the OS on which we are installing

I am finding it difficult to change the selection in the components page of nsis.
The requirement is during installation i get a license agreement page ,if the user agrees then he/she will click on I AGREE ,after the user clicks on I AGREE ,i want to know on which OS the
setup is being installed that is it can be either on a Windows Embedded OS or WinXp/Win7.
So if it is Windows Embedded OS i want to change the installation package and if it is not Windows Embedded OS then the installation package will be different.
I am using MUI ver1 not MUI2 in my project.
Please let me know how this can be achieved.
To test for the OS where the setup is running, you can use macros defined Winver.nsh with those provided with LogicLib.nsh to make elegant tests like this
;Dont't forget to include
!include "LogicLib.nsh" # use of various logic statements
!include "WinVer.nsh" # LogicLib extension for OS tests
A platform test example:
${if} ${AtLeastWin95}
${AndIf} ${AtMostWinME}
;here we are on a pre-win2k
;do something
${elseIf} ${isWin2008}
${orIf} ${AtLeastWin2008R2}
;this is post-win7
;do other thing
${endif}
To change at runtime the components to install, you can use the macros from Sections.nsh:
;if you have
Section "Sample Database" SecApplicationDB
;...
SectionEnd
;you can select or un select by code:
!insertmacro SelectSection ${SecApplicationDB}
;or
!insertmacro UnselectSection ${SecApplicationDB}
WinVer.nsh does not support checking for Embedded NT but you can perform the check yourself:
!include Sections.nsh
!include MUI.nsh
!ifndef VER_SUITE_EMBEDDEDNT
!define VER_SUITE_EMBEDDEDNT 0x00000040
!endif
!insertmacro MUI_PAGE_LICENSE "${__FILE__}"
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE English
Section "Embedded" SID_EMBED
SectionIn RO
SectionEnd
Section "Normal" SID_NORMAL
SectionIn RO
SectionEnd
Function .onInit
System::Call '*(i156,&i152)i.r1'
System::Call 'KERNEL32::GetVersionExA(ir1)'
System::Call '*$1(&i152,&i2.r2)'
System::Free $1
IntOp $2 $2 & ${VER_SUITE_EMBEDDEDNT}
${If} $2 <> 0
!insertmacro SelectSection ${SID_EMBED}
!insertmacro UnselectSection ${SID_NORMAL}
${Else}
!insertmacro UnselectSection ${SID_EMBED}
!insertmacro SelectSection ${SID_NORMAL}
${EndIf}
FunctionEnd

Having InstallDir within IF ELSE block

I try to have the following code from
; The default installation directory
InstallDir $PROGRAMFILES\${PRODUCT_NAME}
to
!include x64.nsh
${If} ${RunningX64}
; The default installation directory
InstallDir $PROGRAMFILES\${PRODUCT_NAME}
${Else}
; The default installation directory
InstallDir $PROGRAMFILES64\${PRODUCT_NAME}
${EndIf}
I get the following error :-
!insertmacro: _If
Error: Can't add entry, no section or function is open!
Error in macro _RunningX64 on macroline 2
Error in macro _If on macroline 9
Error in script "C:\Users\yccheok\Desktop\mysoftware.nsi" on line 17 -- aborting creation process
Is there way I can set the value for InstallDir, within if else block?
If you need a dynamic $InstDir you should not use InstallDir at all but set $InstDir in .onInit:
Installdir ""
!include LogicLib.nsh
!include x64.nsh
Function .onInit
${If} $InstDir == "" ; /D= was not used on the command line
${If} ${RunningX64}
StrCpy $InstDir "c:\foo"
${Else}
StrCpy $InstDir "c:\bar"
${EndIf}
${EndIf}
FunctionEnd
Your current if else block does not make any sense because you are selecting the 32 bit program files on x64 and the 64 bit program files on x86! It is OK to use $PROGRAMFILES64 on x86 so if you always want the "real" program files you can use $PROGRAMFILES64 for all platforms...

Resources