Skip to content
⌘ NSIS Forum Archive

Uninstall if Installed

9 posts

stonkers#

Uninstall if Installed

I'm guessing this is a typical question, but I was unable to find it posted. I want to make my installer uninstall the previous installation if, indeed, it does exist. Is this an automatic thing, or do I need to manually add this to the script? If manual, what do I use to check it's existance. It seems to uninstall, I would just:

Delete "$INSTDIR\Uninst.exe"
DeleteRegKey HKEY_LOCAL_MACHINE "SOFTWARE\AppName"
DeleteRegKey HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\AppName"
RMDir "$INSTDIR"

thanks again...
deguix#
See the archive function "Auto-uninstall old before installing new". It will help you much.
stonkers#
OK, well I tried that and got some sort of uninstall initiation error (or somthing of the sort). So I found the following code and tried it:

Function .onInit
check:
ReadRegStr $1 HKLM \
"Software\Microsoft\Windows\CurrentVersion\Uninstall\${PROGRAM_NAME}" \
"UninstallString"
StrCmp $1 "" done cont

cont:
HideWindow
MessageBox MB_OKCANCEL|MB_ICONEXCLAMATION "${PROGRAM_NAME} \
is already installed.$\n$\r$\n$\r \
Click 'OK' to remove the previous version or 'Cancel' to abandon \
this upgrade." IDOK uninst
Abort
uninst:
ClearErrors
Exec '$1'
; IfErrors cont
uninst_wait:
Sleep 500
FindWindow $0 "" "${PROGRAM_NAME} Uninstall "
IsWindow $0 uninst_wait ""
uninst_finish:
Sleep 500
FindWindow $0 "" "${PROGRAM_NAME} Uninstall: Completed"
; StrCmp $0 "" show_error
IsWindow $0 uninst_finish ""
done:
BringToFront
FunctionEnd

This "kind of" worked, but not really. The first window comes up asking if I want to uninstall, and I click OK. After clicking OK, the second window comes up, but doesn't wait for me to say uninstall, but rather the install window comes up. I can pull the uninstall window from the task bar, but that's not too intuitive. Anything I can do to make this better? Thanks!
Joost Verburg#
I do not recommend you to use that code, it isn't really a good method.

Use the code in the archive page and make sure your code is correct. You should also check http://nsis.sourceforge.net/site/ind...&tx_faq_faq=14
ursus76#
Use the code in the archive page and make sure your code is correct. You should also check http://nsis.sourceforge.net/site/ind...&tx_faq_faq=14
If I use the faq's script(ExecWait '"$INSTDIR\uninstall.exe" _?=$INSTDIR') , then my application's folder can't be totally cleaned up.

If I use this (ExecWait ExecWait '"$INSTDIR\uninst.exe"'), then the uninstaller won't wait for the installer as faq says.

Also, if the user deletes the uninstaller.exe, then none of above will happen.

Any suggestion for this?

Thanks a lot!
Joost Verburg#
You should clean up the uninstaller and the application folder after running the uninstaller.
kichik#
Or simply copy the uninstaller to the temporary directory yourself and execute it with the same command line arguments.