본문 바로가기

NSIS/Reference

[NSIS] 권한 상승

반응형
# https://nsis.sourceforge.io/UAC_plug-in
!macro _UAC_Elevation un
	Function ${un}_UAC_Elevation
		Push $R0
		Exch
		Pop $R0

		uac_tryagain:
		!insertmacro UAC_RunElevated
		${Switch} $0
		${Case} 0
			${IfThen} $1 = 1 ${|} Quit ${|} ;we are the outer process, the inner process has done its work, we are done
			${IfThen} $3 <> 0 ${|} ${Break} ${|} ;we are admin, let the show go on
			${If} $1 = 3 ;RunAs completed successfully, but with a non-admin user
				MessageBox mb_YesNo|mb_IconExclamation|mb_TopMost|mb_SetForeground "This $R0 requires admin privileges, try again" /SD IDNO IDYES uac_tryagain IDNO 0
			${EndIf}
			;fall-through and die
		${Case} 1223
			MessageBox mb_IconStop|mb_TopMost|mb_SetForeground "This $R0 requires admin privileges, aborting!"
			Quit
		${Case} 1062
			MessageBox mb_IconStop|mb_TopMost|mb_SetForeground "Logon service not running, aborting!"
			Quit
		${Default}
			MessageBox mb_IconStop|mb_TopMost|mb_SetForeground "Unable to elevate, error $0"
			Quit
		${EndSwitch}

		SetShellVarContext all
		
		Pop $R0
	FunctionEnd
!macroend
!insertmacro _UAC_Elevation ""
!insertmacro _UAC_Elevation "un."

!macro	UAC_Elevation
	!ifndef __UNINSTALL__
		Push "installer"
		Call _UAC_Elevation
	!else
		Push "uninstaller"
		Call un._UAC_Elevation
	!endif
!macroend

!define UAC_Elevation "!insertmacro UAC_Elevation"
반응형