View Single Post
Old 7th March 2020, 03:01   #5
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,277
PHP Code:

!include LogicLib.nsh
RequestExecutionLevel highest
#InstallDir "" ; Don't set this so we can detect /D


Var IsAdminMode
!macro SetAdminMode
StrCpy $IsAdminMode 1
SetShellVarContext All
${IfThen$InstDir == "" ${|} StrCpy $InstDir "$Programfiles\$(^Name)" ${|}
!
macroend
!macro SetUserMode
StrCpy $IsAdminMode 0
SetShellVarContext Current
${IfThen$InstDir == "" ${|} StrCpy $InstDir "$LocalAppData\Programs\$(^Name)" ${|}
!
macroend

Function .onInit
UserInfo
::GetAccountType
Pop 
$0
${IfThen} $!= "Admin" ${|} Goto setmode_currentuser ${|}

!
insertmacro SetAdminMode
Goto finalize_mode

setmode_currentuser
:
!
insertmacro SetUserMode

finalize_mode
:
FunctionEnd

Function un.onInit
FileOpen 
$"$ExePathr
FileSeek 
$-1 END
FileReadByte 
$$1
FileClose 
$0
UserInfo
::GetAccountType
Pop 
$0
${If} $<> 0
    
!insertmacro SetAdminMode
    
${If} $!= "Admin"
        
MessageBox MB_ICONSTOP "You must be an administrator to uninstall!"
        
Quit
    
${EndIf}
${Else}
    !
insertmacro SetUserMode
${EndIf}
FunctionEnd

Page Directory
Page InstFiles

Section
SetOutPath $InstDir
WriteUninstaller 
"$InstDir\Uninst.exe"
FileOpen $"$InstDir\Uninst.exe" a
FileSeek 
$0 0 END
FileWriteByte 
$0 $IsAdminMode Store the mode
FileClose 
$0
;WriteRegStr ShCtx "Software\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" ....
;
File myapp.exe
SectionEnd

Section Uninstall
;Delete "$InstDir\myapp.exe"
Delete "$InstDir\Uninst.exe"
RMDir $InstDir
DeleteRegKey ShCtx 
"Software\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)"
SectionEnd 

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote