Auto-uninstall old before installing new: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
No edit summary
Line 22: Line 22:
uninst:
uninst:
   ClearErrors
   ClearErrors
   ExecWait '$R0 _?=$INSTDIR' ;Do not copy the uninstaller to a temp file
  ; Copy the uninstaller to a temp location
  GetTempFileName $0
  CopyFiles $R0 $0
  ;Start the uninstaller using the option to not copy itself
   ExecWait '$0 _?=$INSTDIR'


   IfErrors no_remove_uninstaller
   IfErrors no_remove_uninstaller
     ;You can either use Delete /REBOOTOK in the uninstaller or add some code
     ; In most cases the uninstall is successful at this point.
     ;here to remove to remove the uninstaller. Use a registry key to check
     ; You may also consider using a registry key to check whether
     ;whether the user has chosen to uninstall. If you are using an uninstaller
     ; the user has chosen to uninstall. If you are using an uninstaller
     ;components page, make sure all sections are uninstalled.
     ; components page, make sure all sections are uninstalled.
    goto done
   no_remove_uninstaller:
   no_remove_uninstaller:
    MessageBox MB_ICONEXCLAMATION \
    "Unable to remove previous version of ${PROGRAM_NAME}"
    Abort
    
    
done:
done:
  ; remove the copied uninstaller
  Delete '$0'


FunctionEnd
FunctionEnd

Revision as of 21:01, 11 December 2005

Author: codehistorian (talk, contrib)


This is a code snippet you can put into your .onInit function that will detect whether your software is already installed and, if so, allows the user to uninstall it first.

In the source below, we use the define ${PROGRAM_NAME} to be the name of the product as well as the name of the installer/uninstaller. You can either define this define or search/replace it. If you name your uninstaller or registry location differently, you should change those strings accordingly.

Function .onInit
 
  ReadRegStr $R0 HKLM \
  "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PROGRAM_NAME}" \
  "UninstallString"
  StrCmp $R0 "" done
 
  MessageBox MB_OKCANCEL|MB_ICONEXCLAMATION \
  "${PROGRAM_NAME} is already installed. $\n$\nClick `OK` to remove the \
  previous version or `Cancel` to cancel this upgrade." \
  IDOK uninst
  Abort
 
;Run the uninstaller
uninst:
  ClearErrors
  ; Copy the uninstaller to a temp location
  GetTempFileName $0
  CopyFiles $R0 $0
  ;Start the uninstaller using the option to not copy itself
  ExecWait '$0 _?=$INSTDIR'
 
  IfErrors no_remove_uninstaller
    ; In most cases the uninstall is successful at this point.
    ; You may also consider using a registry key to check whether 
    ; the user has chosen to uninstall. If you are using an uninstaller
    ; components page, make sure all sections are uninstalled.
    goto done
  no_remove_uninstaller:
    MessageBox MB_ICONEXCLAMATION \
    "Unable to remove previous version of ${PROGRAM_NAME}"
    Abort
 
done:
  ; remove the copied uninstaller
  Delete '$0'
 
FunctionEnd