Search code examples
installationwindows-installernsis

Check return code (or something else) to ensure MSI has installed correctly


I am using NSIS to install some MSIs. I'm using ExecWait "msiexec /passive /liare+ ${SETUP_LOG_FILE} -i $TEMP\MyMsi.msi". When the MSI is of the same version as an installed app, it fails the installation (“Another version of this product is already installed”), but NSIS continues on as if nothing is wrong. (But the log file reveals the problem.)

How can I check to see if the MSI install failed? If it did fail, what is the correct way to halt the NSIS installation?


Solution

  • Going off of @Wim's answer, here is my solution. (The name of the app I need to install is "Evergreen Programmer", and there is also code to check if the CPU is 32- or 64-bit.) I don't like the way Abort makes the GUI look, though (the user has to click Cancel):

    screenshot showing the result of using Abort

    !include "x64.nsh"
    
    Function CheckReturnCode
      DetailPrint "MSI return code was $0"  
      ${If} $0 != 0 
        Abort "There was a problem installing the application."
      ${EndIf}
    FunctionEnd
    
    Section "FrameworkAndApp" SecFrameworkApp
    
      SetOutPath "$TEMP"
      File /oname=EvergreenProgrammerSetup.msi "${SETUP_FILE}"
      File /oname=EvergreenProgrammerSetup64.msi "${SETUP_FILE_64}"
    
    InstallEvergreenProgrammer:
      Push "Starting Evergreen Programmer Install Version ${MAJOR_VERSION}.${MINOR_VERSION}.${REVISION}"
      Call DebugLog
      DetailPrint "Starting Evergreen Programmer Install Version ${MAJOR_VERSION}.${MINOR_VERSION}.${REVISION}"
      IfSilent InstallAppWithNoProgressBar
    ${If} ${RunningX64}
      DetailPrint "64-bit detected"
      ExecWait "msiexec /passive /liare+ ${SETUP_LOG_FILE} -i $TEMP\EvergreenProgrammerSetup64.msi" $0
    ${Else}
      ExecWait "msiexec /passive /liare+ ${SETUP_LOG_FILE} -i $TEMP\EvergreenProgrammerSetup.msi" $0
    ${EndIf}
      Call CheckReturnCode
      SetRebootFlag true
      Goto EndInstall
    
    InstallAppWithNoProgressBar:
    ${If} ${RunningX64}
      DetailPrint "64-bit detected"
      ExecWait "msiexec /quiet /liare+ ${SETUP_LOG_FILE} -i $TEMP\EvergreenProgrammerSetup64.msi" $0
    ${Else}
      ExecWait "msiexec /quiet /liare+ ${SETUP_LOG_FILE} -i $TEMP\EvergreenProgrammerSetup.msi" $0
    ${EndIf}
      Call CheckReturnCode
      SetRebootFlag true
      Goto EndInstall
    
    EndInstall:
      IfRebootFlag PromptForReboot
      Return
    PromptForReboot:
      IfSilent SkipReboot
      MessageBox MB_OK "The application will not function correctly without a reboot or log off."
    
    SkipReboot:
    
    SectionEnd