Search code examples
windows-installernsis

When uninstalling the windows registry entry using NSIS, do we need to remove each and every entry from the windows registry?


My requirement is: When I run NSIS installer, First it should check if the user is having administrative user Privileges and if the user is having Privileges then while installing, the application should write the path and the values in the windows registry.

To check the administrative Privileges i wrote the below code snippet in the

.onInit

RequestExecutionLevel admin

Function .onInit
UserInfo::GetAccountType
pop $0
${If} $0 != "admin"
MessageBox mb_iconstop "Administrator rights required!"
SetErrorLevel 740 ;ERROR_ELEVATION_REQUIRED
Quit
${Else}
MessageBox MB_OK "User is having Administrative Permissions"
${EndIf}

FunctionEnd

If the user is having permissions I wrote the below code to set the windows registry entries.

;--------------------------------
; The stuff to install
Section "RegistryTest (required)"

  SectionIn RO

  ; Set output path to the installation directory. Here is the path C:\Program Files\RegistryTest
  SetOutPath $INSTDIR

  ; Write the installation path into the registry

  ;Adding Registry entries under "Dialog"
   WriteRegDWORD HKLM SOFTWARE\RegistryTest\Dialog "AppDataDlg" "1"
   WriteRegStr HKLM SOFTWARE\RegistryTest\Dialog "ReplaceTestWebPage" "http://www.Test.com/tools/upgrade_selector/index.cfm?Localize=true"
   WriteRegStr HKLM SOFTWARE\RegistryTest\Dialog "UpgradeUpsUrl" "http://www.Test.com/tools/upgrade_selector/Testindex.cfm?Localize=true"

  ;Adding Registry entries under "EventLogging"
   WriteRegStr HKLM SOFTWARE\RegistryTest\EventLogging "ImagePath" "$INSTDIR\eventlog.dat"

   ;Adding Registry entries under "Notifications"
    WriteRegDWORD HKLM SOFTWARE\RegistryTest\Notification "Notification Sounds Enabled" "1"

    ;Adding Registry entries under "POS"
    WriteRegDWORD HKLM SOFTWARE\RegistryTest\POS "Enabled" "0"
    WriteRegStr HKLM SOFTWARE\RegistryTest\POS "HttpClient" "TestData.exe"
    WriteRegStr HKLM SOFTWARE\RegistryTest\POS "HttpPacketVer" "1"
    WriteRegStr HKLM SOFTWARE\RegistryTest\POS "HttpServer" "updates.test.com"
    WriteRegStr HKLM SOFTWARE\RegistryTest\POS "HttpServerUrl" "/Test/pos/tetstatus.cfm"
    WriteRegStr HKLM SOFTWARE\RegistryTest\POS "ProductCode" "0"
    WriteRegStr HKLM SOFTWARE\RegistryTest\POS "ProductVer" "00"
    WriteRegStr HKLM SOFTWARE\RegistryTest\POS "ZipCode" ""
    
    
  ; Write the uninstall keys for Windows
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\RegistryTest" "DisplayName" "RegistryTest"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\RegistryTest" "UninstallString" '"$INSTDIR\uninstall.exe"'
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\RegistryTest" "NoModify" 1
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\RegistryTest" "NoRepair" 1
  WriteUninstaller "uninstall.exe"

SectionEnd

Below are clarifications I am having:

  1. Is the Administrative permissions condition i wrote works for all the operating systems or is there any specific operating systems?

  2. Is the code that i have written is first i am checking the permissions and then writing it into the registry. Is it the correct way? Please suggest if there is any changes required.

  3. Here i found one observation:

My user is Admin user and if we use "HKLM" windows registry, then when run "NSIS" installer normally or run as administrator it is treating it as administrative user and going to else condition and showing the message as "User is having Administrative Permissions"

If i use "HKCU" windows registry, then when run "NSIS" installer normally it is going to the "If" condition and showing "Administrator rights required!". And if I run "NSIS" as run as administrator, it is treating it as administrative user and going to else condition and showing the message as "User is having Administrative Permissions".

Here, Why it is happening differently for "HKLM" and "HKCU"? Is it because "HKLM" will have admin privis and "HKCU" won't have permissions.

  1. In the Uninstall section i am not uninstalling each section in the windows registry instead directly using uninstall.exe. But When uninstalling it is removing all the entries from the windows registy. So, here is it not required to uninstall each registry entry?

Solution

  • 1) It works on all systems.

    2) Aborting in .onInit is OK

    3) You should not write to HKCU if you are running as a administrator. UAC can cause the installer to run as the "wrong" user and you end up writing to the administrators HKCU instead of the normal user.

    4) DeleteRegKey will delete all values in a key (and subkeys), you don't have to manually delete each item.