<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://nsis.sourceforge.io/mediawiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=DavidWoods</id>
	<title>NSIS Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://nsis.sourceforge.io/mediawiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=DavidWoods"/>
	<link rel="alternate" type="text/html" href="https://nsis.sourceforge.io/Special:Contributions/DavidWoods"/>
	<updated>2026-04-06T21:03:23Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.17</generator>
	<entry>
		<id>https://nsis.sourceforge.io/mediawiki/index.php?title=Auto-uninstall_old_before_installing_new&amp;diff=24968</id>
		<title>Auto-uninstall old before installing new</title>
		<link rel="alternate" type="text/html" href="https://nsis.sourceforge.io/mediawiki/index.php?title=Auto-uninstall_old_before_installing_new&amp;diff=24968"/>
		<updated>2017-07-07T20:14:23Z</updated>

		<summary type="html">&lt;p&gt;DavidWoods: Suggested code that does not assume previous install was in the default directory&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{PageAuthor|codehistorian}}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;highlight-nsis&amp;gt;&lt;br /&gt;
Function .onInit&lt;br /&gt;
&lt;br /&gt;
  ReadRegStr $R0 HKLM \&lt;br /&gt;
  &amp;quot;Software\Microsoft\Windows\CurrentVersion\Uninstall\${PROGRAM_NAME}&amp;quot; \&lt;br /&gt;
  &amp;quot;UninstallString&amp;quot;&lt;br /&gt;
  StrCmp $R0 &amp;quot;&amp;quot; done&lt;br /&gt;
&lt;br /&gt;
  MessageBox MB_OKCANCEL|MB_ICONEXCLAMATION \&lt;br /&gt;
  &amp;quot;${PROGRAM_NAME} is already installed. $\n$\nClick `OK` to remove the \&lt;br /&gt;
  previous version or `Cancel` to cancel this upgrade.&amp;quot; \&lt;br /&gt;
  IDOK uninst&lt;br /&gt;
  Abort&lt;br /&gt;
  &lt;br /&gt;
;Run the uninstaller&lt;br /&gt;
uninst:&lt;br /&gt;
  ClearErrors&lt;br /&gt;
  ExecWait &#039;$R0 _?=$INSTDIR&#039; ;Do not copy the uninstaller to a temp file&lt;br /&gt;
&lt;br /&gt;
  IfErrors no_remove_uninstaller done&lt;br /&gt;
    ;You can either use Delete /REBOOTOK in the uninstaller or add some code&lt;br /&gt;
    ;here to remove the uninstaller. Use a registry key to check&lt;br /&gt;
    ;whether the user has chosen to uninstall. If you are using an uninstaller&lt;br /&gt;
    ;components page, make sure all sections are uninstalled.&lt;br /&gt;
  no_remove_uninstaller:&lt;br /&gt;
  &lt;br /&gt;
done:&lt;br /&gt;
&lt;br /&gt;
FunctionEnd&lt;br /&gt;
&amp;lt;/highlight-nsis&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following variant lets the uninstaller remove itself, so the IfErrors block is not needed any more:&lt;br /&gt;
&amp;lt;highlight-nsis&amp;gt;&lt;br /&gt;
  Exec $INSTDIR\uninst.exe ; instead of the ExecWait line&lt;br /&gt;
&amp;lt;/highlight-nsis&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NOTE:  Both previous methods assume the program was installed in the default directory, which may not be correct.  Here&#039;s what I use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;highlight-nsis&amp;gt;&lt;br /&gt;
  uninst:&lt;br /&gt;
    ClearErrors&lt;br /&gt;
    Exec $R0&lt;br /&gt;
  done:&lt;br /&gt;
&amp;lt;/highlight-nsis&amp;gt; &lt;br /&gt;
&lt;br /&gt;
[[Category:System Related Functions]]&lt;/div&gt;</summary>
		<author><name>DavidWoods</name></author>
	</entry>
</feed>