Search code examples
nsisconditional-statements

NSIS conditional statement


I am writing a script code which installs 3 drivers. Every time that a driver is installed I get a return code of '256' or '1' if the installation was successful, or a different return code if the installation failed. I need to check that all installations finished sucessfully, so I use the following code:

${If} "$2" == "1" ${OrIf} "$1" == "256" ${AndIf} "$2" == "256" ${OrIf} "$2" == "1" ${AndIf} "$3" == "256" ${OrIf} "$3" == "1" MessageBox MB_OK "Installation was successful!" ${Else} MessageBox MB_OK "Sorry, an error occurred during installation." ${EndIf}

Even if a driver installation fails I receive the message "Installation was successful!". Could someone help me find the mistake in the above code?


Solution

  • In your example code the first ${If} is using $2 when it should probably check $1.

    I don't think you should use OrIf and AndIf in the same if block like this, the results are probably undefined.

    It is also possible to write your own custom LogicLib operators:

    !include LogicLib.nsh
    !macro _MyCheckExitcodeSuccess _a _b _t _f
        !if `${_f}` == ``
            !undef _f
            !define _f +2 
        !endif
        IntCmp ${_b} 1 +2
        IntCmp ${_b} 256 `${_t}` `${_f}` `${_f}`
        !if `${_t}` != ``
            Goto `${_t}`
        !endif
    !macroend
    !define MyCheckExitcodeSuccess `"" MyCheckExitcodeSuccess`
    
    StrCpy $1 1
    StrCpy $2 256
    ${If} ${MyCheckExitcodeSuccess} $1
    ${AndIf} ${MyCheckExitcodeSuccess} $2
        MessageBox mb_ok "All OK"
    ${Else}
        MessageBox mb_ok "Something failed"
    ${EndIf}