
!define PRODUCT_NAME "My Application"
!define PRODUCT_SHORT_NAME "App"

!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"

;uninstall previous version first
var OLD_UNINSTALL_APP
function .onInit
  ;check for previously installed versions in the registry
  ReadRegStr $OLD_UNINSTALL_APP "${PRODUCT_UNINST_ROOT_KEY}" "${PRODUCT_UNINST_KEY}" "UninstallString"
  StrCmp $OLD_UNINSTALL_APP "" endFunction  ;exit function if there was not a previous version installed

  ;prompt the user to uninstall previous versions - if they do not wish to uninstall, abort the new installation
  MessageBox MB_ICONSTOP|MB_YESNO "You must uninstall all previous versions of the ${PRODUCT_SHORT_NAME} before \
             you can install a new version. Do you want to completely remove the old version?" IDNO abortInstall
  ExecWait '"$OLD_UNINSTALL_APP" _?=$INSTDIR'  ;extra parameter causes this app to wait while uninstall executes

  ;check that the user completed the uninstallation by examining the registry
  ReadRegStr $OLD_UNINSTALL_APP "${PRODUCT_UNINST_ROOT_KEY}" "${PRODUCT_UNINST_KEY}" "UninstallString"
  StrCmp $OLD_UNINSTALL_APP "" endFunction abortInstall

  ;abort the installation
abortInstall:
  Abort

endFunction:
functionEnd

