Testing uses the NSIS\Examples\Modern UI\Basc.nsi example since it was used by the original application and should be useful for others following this thread.
In summary, the key issue is the installer needs to change the registry but a
user only has access to HKEY_CURRENT_USER (HKCU) and changing HKEY_LOCAL_MACHINE (HKLM) requires
admin access. As noted above to choose install type at run-time:
Code tested is at the bottom of this post.
Test Results:
Quote:
To find out if your account has administrator privilege follow the steps below:
* Press Windows + R.
* Type netplwiz then hit Enter.
* Verify from the pop-up window if the account your using has administrator privilege.
|
Admin user:
"Do you want to allow this app from an unknown publisher to make changes to your device?"
N - Installer terminates, Y - Installer runs
Destination: C:\Program Files (x86)\Modern UI Test
Output Folder: C:\Program Files (x86)\Modern UI Test - Verified
Created uninstaller: C:\Program Files (x86)\Modern UI TestUninstall.exe - Verified
Registry: Computer\HKEY_USERS\S-1-5-21-293778308-4012132540-2240235571-1001\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Compatibility Assistant\Store
Uninstall removes installation as above.
Comment:
* No option for Admin user to just install for current user
* Non-admin install not tested
PHP Code:
;NSIS Modern User Interface
;Basic Example Script
;Written by Joost Verburg
;Modified by Anders and flywire
;--------------------------------
;Include Modern UI
!include "MUI2.nsh"
!include LogicLib.nsh
;--------------------------------
;General
;Name and file
Name "Modern UI Test"
OutFile "BasicCtx.exe"
Unicode True
;Default installation folder
# InstallDir "$LOCALAPPDATA\Modern UI Test"
#InstallDir "" ; Don't set this so we can detect /D
;Get installation folder from registry if available
# InstallDirRegKey HKLM "Software\Modern UI Test" ""
;Request application privileges for Windows Vista
# RequestExecutionLevel user
RequestExecutionLevel highest
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
;--------------------------------
;Interface Settings
!define MUI_ABORTWARNING
;--------------------------------
;Pages
!insertmacro MUI_PAGE_LICENSE "${NSISDIR}\Docs\Modern UI\License.txt"
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
;--------------------------------
;Languages
!insertmacro MUI_LANGUAGE "English"
;--------------------------------
;Installer Sections
Section "Dummy Section" SecDummy
SetOutPath "$INSTDIR"
;ADD YOUR OWN FILES HERE...
;Store installation folder
# WriteRegStr HKCU "Software\Modern UI Test" "" $INSTDIR
;Create uninstaller
WriteUninstaller "$INSTDIR\Uninstall.exe"
FileOpen $0 "$InstDir\Uninstall.exe" a
FileSeek $0 0 END
FileWriteByte $0 $IsAdminMode ; Store the mode inside the uninstaller
FileClose $0
WriteRegStr ShCtx "Software\$(^Name)" "" $INSTDIR
;File myapp.exe
SectionEnd
;--------------------------------
;Descriptions
;Language strings
LangString DESC_SecDummy ${LANG_ENGLISH} "A test section."
;Assign language strings to sections
!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
!insertmacro MUI_DESCRIPTION_TEXT ${SecDummy} $(DESC_SecDummy)
!insertmacro MUI_FUNCTION_DESCRIPTION_END
;--------------------------------
;Uninstaller Section
Section "Uninstall"
;ADD YOUR OWN FILES HERE...
Delete "$INSTDIR\Uninstall.exe"
RMDir "$INSTDIR"
#DeleteRegKey /ifempty HKCU "Software\Modern UI Test"
DeleteRegKey /ifempty ShCtx "Software\$(^Name)"
SectionEnd
Function .onInit
UserInfo::GetAccountType
Pop $0
${IfThen} $0 != "Admin" ${|} Goto setmode_currentuser ${|}
!insertmacro SetAdminMode
Goto finalize_mode
setmode_currentuser:
!insertmacro SetUserMode
finalize_mode:
FunctionEnd
Function un.onInit
FileOpen $0 "$ExePath" r
FileSeek $0 -1 END
FileReadByte $0 $1
FileClose $0
UserInfo::GetAccountType
Pop $0
${If} $1 <> 0
!insertmacro SetAdminMode
${If} $0 != "Admin"
MessageBox MB_ICONSTOP "You must be an administrator to uninstall!"
Quit
${EndIf}
${Else}
!insertmacro SetUserMode
${EndIf}
FunctionEnd