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?

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

1 Answers1

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

Anders
  • 97,548
  • 12
  • 110
  • 164
  • 1
    "$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. – Cheok Yan Cheng Nov 29 '11 at 01:23
  • 1
    @YanChengCHEOK No it is not important but /D will override InstallDir so staying compatible does not hurt ;) – Anders Nov 29 '11 at 02:44
  • 1
    Thanks! The problem with me was that I was trying to do this outside `.onInit`. – thameera Oct 06 '14 at 01:36