Having InstallDir within IF ELSE block
Asked Answered
B

1

6

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?

Bresnahan answered 28/11, 2011 at 10:1 Comment(0)
R
10

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...

Reverence answered 28/11, 2011 at 17:5 Comment(3)
"$PROGRAMFILES64 for all platforms" -> this is a pretty nice hint. By the way, is "/D= was not used on the command line" handling case important, as my users most of the time will just double click on the installer.Bresnahan
@YanChengCHEOK No it is not important but /D will override InstallDir so staying compatible does not hurt ;)Reverence
Thanks! The problem with me was that I was trying to do this outside .onInit.Tailpiece

© 2022 - 2024 — McMap. All rights reserved.