Search code examples
nsisprivilegesuninstallation

NSIS uninstaller privileges according to installer


I have an NSIS installer for an application, which can be run as normal user. But if the user wants to install into the "Program Files" directory, it can still be accomplished by starting the installer with administrator privileges.

Now I have the problem, that the uninstaller is started with user privileges by default, even if the installation took place as administrator. This causes the uninstallation to silently fail. Even worse: It even states that the uninstall process was successful without being able to delete any files.

My question is: Is it possible to create an uninstaller during the installation, which requires (or better: requests itself with) the same privileges as the installation process?


Solution

  • You would have to implement this check yourself. You can check if you are admin in the installer with the UserInfo plugin and then store the result in a .ini, the registry or append the info to the uninstaller.exe:

    InstallDir $temp\instdir
    
    Section
    
    UserInfo::GetAccountType
    Pop $0
    StrCmp $0 "Admin" 0 +2
    StrCpy $0 1
    IntOp $0 $0 & 1 ; $0 is now 1 if admin or 0 if not
    
    SetOutPath $InstDir
    WriteUninstaller "$InstDir\Uninstall.exe"
    FileOpen $1 "$InstDir\Uninstall.exe" a
    FileSeek $1 0 END
    FileWriteByte $1 $0
    FileClose $1
    
    SectionEnd
    
    
    Section Uninstall
    
    FileOpen $1 "$ExePath" r
    FileSeek $1 -1 END
    FileReadByte $1 $0
    FileClose $1
    DetailPrint "Installer was admin: $0"
    
    SectionEnd