Search code examples
nsis

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?


Solution

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