Search code examples
parametersinstallationnsis

How can I know if /D parameter was passed into NSIS-based installer


Is there any flag or other option in NSIS to see that the default $INSTDIR variable was changed via /D parameter passed to the installer?

I tried to do that:

1. setup.exe /D=C:\Temp
2. ${GetParameters} $R0
3. And see that $R0 is empty

Any advice?


Solution

  • Before .onInit is executed, $InstDir is set by InstallDir, then if the value defined by InstallDirRegKey exists it will override InstallDir. Finally, if the user used /D, $InstDir is set to that parameter. This is OK for most installers because you don't really need to know how $InstDir was set.

    If you are using RequestExecutionLevel highest and your installer supports installing for a single user or all users depending on the elevation status then this is not OK. The only way around this is to not use InstallDir*

    ; DO NOT USE: InstallDir
    ; DO NOT USE: InstallDirRegKey
    
    !include LogicLib.nsh
    
    Function .onInit
    ${If} $InstDir != ""
      ; /D was used
    ${Else}
      ; Set some default
      StrCpy $InstDir "$ProgramFiles\foo\bar"
    ${EndIf}
    FunctionEnd
    

    Alternatively you could maybe use System::Call kernel32::GetCommandLine()t.r0 and parse $0 looking for /D at the end...