Skip to content
⌘ NSIS Forum Archive

Registry Not Updated after DeleteRegKey

4 posts

iamoha#

Registry Not Updated after DeleteRegKey

I have an NSI installer for my program and a batch script.
The NSI checks if an older version exists by checking if a product key exists in the registry and if so, then uninstall the old version (run the related uninst.exe). The installer eventually writes the product key in the registry.
The batch script checks if an older version of my program is installed by checking if the product key exists in the registry, and if so, then uninstall the old version (running the related uninst.exe), do some cleaning, and then install the new version. However, when installing the new version, the NSI installer detect the product key in the registry, which is supposed to be deleted during uninstallation, then tried to run the uninst.exe (path saved in the registry) and failed since the file doesn't exist. This doesn't happen all the times, but sometimes.

To reproduce the same issue, I wrote a small NSI and batch script samples. The batch fail from time to time (detect a deleted registry key and displays the value in a message).
The NSI code (test_registry.nsi):

!include "MUI.nsh"

!define productKeyPath "Software\Microsoft\Windows\CurrentVersion\Installer\UpgradeCodes"
!define productKey "123456789"
!define MUI_ABORTWARNING
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"

InstallDir "$COMMONFILES\Testing"

Section "MainSection" SEC01
SetOutPath "$INSTDIR"
SectionEnd

Function .onInit
SetOutPath "$INSTDIR"
ClearErrors
EnumRegValue $R2 HKLM "${productKeyPath}${productKey}" 0
IfErrors notExists
MessageBox MB_OK "Key exists and value is $R2"
SetErrorLevel 7
notExists:
WriteRegStr HKLM "${productKeyPath}${productKey}" "myKey" "Hello"
FunctionEnd

Section -Post
WriteUninstaller "$INSTDIR\uninst.exe"
SectionEnd

Function un.onInit
IfSilent +3
MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "Are you sure you want to uninstall?" IDYES +2
Abort
FunctionEnd

Section Uninstall
Delete "$INSTDIR\uninst.exe"
DeleteRegKey HKLM "${productKeyPath}${productKey}" ""
SectionEnd

The batch script:
for /L %%A in (1,1,20) do (
"test_registry.exe" /S
echo %ErrorLevel%
"C:\Program Files (x86)\Common Files\Testing\uninst.exe" /S
echo %ErrorLevel%
"test_registry.exe" /S
echo %ErrorLevel%
"C:\Program Files (x86)\Common Files\Testing\uninst.exe" /S
echo %ErrorLevel%
)​

Putting a delay between the uninstallation and the installation can help but not a guarantee and I don't want to use a delay. It looks like the registry is not updated after calling the DeleteRegKey sometimes, any solution?
Anders#
You are not waiting for the uninstaller to finish.

@echo off
setlocal enabledelayedexpansion
set inst=test_registry.exe
set "instdir=C:\Program Files (x86)\Common Files\Testing"
for /L %%A in (1,1,33) do (
    "%inst%" /S
    echo I=!errorlevel!
    "!instdir!\uninst.exe" /S _?=!instdir!
    echo U=!errorlevel!
) 
iamoha#
Thank you, Anders, for the reply. Can you please provide more details on why your code is waiting for the uninstaller to finish while this code is not:

for /L %%A in (1,1,33) do (
"C:\Program Files (x86)\Common Files\Testing\uninst.exe" /S
"test_registry.exe" /S
)