I tried embedding this procedure into the OnInit function, but it doesn't seem to work here. However, I did install an older version of my software and I did create a string called InstallDir in HKLM\Software\USBsyncer with the value "C:\Program Files\MediaSync\". Why is it not working?
; USBsyncer-0.3.nsi
;
; This script is based on example1.nsi, but it remembers the directory,
; has uninstall support and (optionally) installs start menu shortcuts.
;
; It will install example2.nsi into a directory that the user selects,
;--------------------------------
;Include Modern UI
!include "MUI.nsh"
;--------------------------------
; The name of the installer
Name "USBsyncer v0.3"
XPStyle on
ShowInstDetails show
ShowUninstDetails show
ReserveFile "ReadmePage.ini"
!insertmacro MUI_RESERVEFILE_INSTALLOPTIONS
!define MUI_FINISHPAGE_SHOWREADME "readme.txt"
!define MUI_FINISHPAGE_SHOWREADME_TEXT "Show Readme (recommended)"
!define MUI_FINISHPAGE_RUN "$INSTDIR\USBsyncer.exe"
!define MUI_FINISHPAGE_RUN_TEXT "Run USBsyncer"
!define MUI_FINISHPAGE_LINK "USBsyncer Homepage"
!define MUI_FINISHPAGE_LINK_LOCATION "http://usbsyncer.ueuo.com/"
; The file to write
OutFile "../Installer Builds/USBsyncer-output.exe"
; The default installation directory
InstallDir "$PROGRAMFILES\USBsyncer v0.3\"
; Registry key to check for directory (so if you install again, it will
; overwrite the old one automatically)
; InstallDirRegKey HKLM "Software\NSIS_Example2" "Install_Dir"
;--------------------------------
!define MUI_HEADERIMAGE
!define MUI_HEADERIMAGE_RIGHT
!define MUI_WELCOMEFINISHPAGE_BITMAP "header_left.bmp"
!define MUI_UNWELCOMEFINISHPAGE_BITMAP "header_left.bmp"
!define MUI_HEADERIMAGE_BITMAP "header.bmp"
!define MUI_ABORTWARNING
!define MUI_UNABORTWARNING
Function .onInit
# the plugins dir is automatically deleted when the installer exits
InitPluginsDir
File /oname=$PLUGINSDIR\splash.bmp "splash.bmp"
#optional
#File /oname=$PLUGINSDIR\splash.wav "C:\myprog\sound.wav"
advsplash::show 5000 600 400 -1 $PLUGINSDIR\splash
Pop $0 ; $0 has '1' if the user closed the splash screen early,
; '0' if everything closed normally, and '-1' if some error occurred.
Delete $PLUGINSDIR\splash.bmp
!insertmacro MUI_INSTALLOPTIONS_EXTRACT_AS "ReadmePage.ini" "ReadmePage.ini"
ReadRegStr $0 HKLM Software\USBsyncer\ "InstallDir"
IfErrors 0 +2
ReadRegStr $0 HKLM Software\MediaSync\ "InstallDir"
IfFileExists $0\MediaSync.exe 0 +6
MessageBox MB_OK "An old version of USBsyncer or MediaSync seems to be installed. Press OK to run its uninstaller."
IfFileExists $0\Uninstall.exe 0 +2
ExecWait '"$0\Uninstall.exe"'
IfFileExists $0\Setup.exe 0 +2
ExecWait '"$0\Setup.exe" /remove'
IfFileExists $0\USBsyncer.exe 0 +6
MessageBox MB_OK "An old version of USBsyncer or MediaSync seems to be installed. Press OK to run its uninstaller."
IfFileExists $0\Uninstall.exe 0 +2
ExecWait '"$0\Uninstall.exe"'
IfFileExists $0\Setup.exe 0 +2
ExecWait '"$0\Setup.exe" /remove'
FunctionEnd
;--------------------------------
; Pages
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_LICENSE "license.txt"
Page custom ReadmePage
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_COMPONENTS
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"
;--------------------------------
; Readme
LangString TEXT_IO_TITLE ${LANG_ENGLISH} "USBsyncer Information"
LangString TEXT_IO_SUBTITLE ${LANG_ENGLISH} "Please read this to learn more about the way USBsyncer works."
Function ReadmePage
!insertmacro MUI_HEADER_TEXT "$(TEXT_IO_TITLE)" "$(TEXT_IO_SUBTITLE)"
!insertmacro MUI_INSTALLOPTIONS_DISPLAY "ReadmePage.ini"
FunctionEnd
;--------------------------------
; License
LicenseData license.txt
LicenseForceSelection checkbox
;--------------------------------
; The stuff to install
InstType "Full (recommended)"
InstType "Minimal"
Section "USBsyncer Core" Core
SectionIn RO 1 2
; Set output path to the installation directory.
SetOutPath $INSTDIR
; Files in the root dir
File "USBsyncer.exe"
File "readme.txt"
File "license.txt"
; Files in the bin dir
SetOutPath $INSTDIR\bin
File "bin\USBwatcher.exe"
; Files in the images dir
SetOutPath $INSTDIR\images
File "images\appheader.gif"
File "images\busy.ico"
File "images\idle.ico"
; Write the installation path into the registry
WriteRegStr HKLM SOFTWARE\USBsyncer "Install_Dir" "$INSTDIR"
; Write the uninstall keys for Windows
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\USBsyncer0.3" "DisplayName" "USBsyncer"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\USBsyncer0.3" "UninstallString" '"$INSTDIR\uninstall.exe"'
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\USBsyncer0.3" "NoModify" 1
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\USBsyncer0.3" "NoRepair" 1
WriteUninstaller "uninstall.exe"
SectionEnd
Section "USBsyncer Updater" Updater
SectionIn 1
; Set output path to the installation directory.
SetOutPath $INSTDIR
; Put file there
File "USBupdater.exe"
; Write the installation path into the registry
WriteRegStr HKLM SOFTWARE\USBsyncer "Install_Dir" "$INSTDIR"
; Write the uninstall keys for Windows
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\USBsyncer0.3" "DisplayName" "USBsyncer"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\USBsyncer0.3" "UninstallString" '"$INSTDIR\uninstall.exe"'
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\USBsyncer0.3" "NoModify" 1
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\USBsyncer0.3" "NoRepair" 1
WriteUninstaller "uninstall.exe"
SectionEnd
Section "Start with Windows" EnableOnBootup
SectionIn 1 2
CreateShortCut "$SMSTARTUP\USBsyncer.lnk" "$INSTDIR\USBsync.exe" "" "$INSTDIR\USBsync.exe" 0
SectionEnd
Section "Start Menu Shortcuts" Shortcuts
SectionIn 1 2
CreateDirectory "$SMPROGRAMS\USBsyncer v0.3"
CreateShortCut "$SMPROGRAMS\USBsyncer v0.3\Start USBsyncer v0.3.lnk" "$INSTDIR\USBsyncer.exe" "" "$INSTDIR\USBsyncer.exe" 0
CreateShortCut "$SMPROGRAMS\USBsyncer v0.3\Synchronize and quit.lnk" "$INSTDIR\USBsyncer.exe" "/syncandquit" "$INSTDIR\USBsyncer.exe" 0
CreateShortCut "$SMPROGRAMS\USBsyncer v0.3\Uninstall USBsyncer v0.3.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0
SectionEnd
;--------------------------------
; Other stuff
;--------------------------------
; Uninstaller
Section "un.USBsyncer" unProgram
SectionIn RO
; Remove registry keys
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\USBsyncer0.3"
DeleteRegKey HKLM SOFTWARE\USBsyncer
; Remove files and uninstaller
Delete $INSTDIR\USBsyncer.exe
Delete $INSTDIR\USBupdater.exe
Delete $INSTDIR\license.txt
Delete $INSTDIR\readme.txt
Delete $INSTDIR\Uninstall.exe
Delete $INSTDIR\bin\*.*
Delete $INSTDIR\images\*.*
RMDir "$INSTDIR\bin\"
RMDir "$INSTDIR\images\"
; Remove shortcuts, if any
Delete "$SMPROGRAMS\USBsyncer v0.3\*.*"
Delete "$SMSTARTUP\USBsyncer.lnk"
; Remove Start Menu folder
RMDir "$SMPROGRAMS\USBsyncer v0.3\"
SectionEnd
Section "un.Delete settings" unPrgrmFldr
RMDir "$INSTDIR"
SectionEnd
;--------------------------------
;Descriptions
; Installer component description strings
LangString DESC_Core ${LANG_ENGLISH} "The main component of USBsyncer."
LangString DESC_Updater ${LANG_ENGLISH} "A simple updater to check for new versions."
LangString DESC_EnableOnBootup ${LANG_ENGLISH} "This is required for USBsyncer to work properly."
LangString DESC_Shortcuts ${LANG_ENGLISH} "Creates shortcuts for USBsyncer."
; Same for uninstaller
LangString DESC_unProgram ${LANG_ENGLISH} "Uninstall USBsyncer and all its components."
LangString DESC_unPrgrmFldr ${LANG_ENGLISH} "Remove the USBsyncer program directory and settings."
; Link strings to installer components
!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
!insertmacro MUI_DESCRIPTION_TEXT ${Core} $(DESC_Core)
!insertmacro MUI_DESCRIPTION_TEXT ${Updater} $(DESC_Updater)
!insertmacro MUI_DESCRIPTION_TEXT ${EnableOnBootup} $(DESC_EnableOnBootup)
!insertmacro MUI_DESCRIPTION_TEXT ${Shortcuts} $(DESC_Shortcuts)
!insertmacro MUI_FUNCTION_DESCRIPTION_END
; Once again, same for uninstaller
!insertmacro MUI_UNFUNCTION_DESCRIPTION_BEGIN
!insertmacro MUI_DESCRIPTION_TEXT ${unProgram} $(DESC_unProgram)
!insertmacro MUI_DESCRIPTION_TEXT ${unPrgrmFldr} $(DESC_unPrgrmFldr)
!insertmacro MUI_UNFUNCTION_DESCRIPTION_END
;--------------------------------