Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Compiled script with option to run as admin or not (for UAC)


  • Please log in to reply
2 replies to this topic
daorc
  • Members
  • 177 posts
  • Last active:
  • Joined: 18 Oct 2006
Hi everyone,

This script allows its self to be run either with or without admin privileges (for UAC), giving the user the option when it starts. If the user selects to have an admin version of the script running then it reloads as administrator.

It must be compiled to work.

Thanks to aw1ace and Chris (referenced in code).

#singleinstance,force ;required because creating new elevated instance of this script vs. exiting the old instance is a race condition

if not a_iscompiled
	{
	msgbox Script must be compiled for this to work
	Exitapp
	}
	
loop, %0% ;tell us what parameters are passed to script (for demonstration purposes)
	{
	msgbox,% "param" A_Index " = " %A_Index%
	}
	

;------------------------------------------------------------------------------------------------

;=============
;Choose admin level (GUI)
;=============
	
if NOT a_isadmin
	{


	gui,add,button,vElevateButton w100 h100,Elevate
	gui,add,button,vNoElevateButton w100 h100,NoElevate

	Button_SetShield("ElevateButton")
	gui,show

	return
	}

;=============
;Rest of script
;=============

admin_choice_made:
msgbox script would now continue (admin=%A_isadmin%)
; Place the rest of your script here
exitapp
return

;---------------------------------------------------------------------------

;=============
;Buttons
;=============

ButtonElevate:
Process_elevate()
return

ButtonNoElevate:
goto admin_choice_made
return


;=============
;Functions
;=============

;=====
;Button_SetSheild
;=====

Button_SetShield(ButtonVar,Elevate=True) ;adapted from aw1ace's code (http://www.autohotkey.com/forum/topic50170.html)
	{
	static BCM_SETSHIELD=0x160C ;removed "BCM_FIRST=0x3" from aw1ace's version, because it didn't work for me with it in, and I don't really understand what it does!
	GuiControlGet,ButtonHwnd,Hwnd,%ButtonVar%
	SendMessage,BCM_SETSHIELD,0,Elevate,,ahk_id %ButtonHwnd%
    return ErrorLevel
	}

;=====
;Elevate to admin
;=====
	
Process_elevate() ;adapted from Chris' code (http://www.autohotkey.com/forum/topic18062.html)
   { 
	global ;required so it can pass params to new instance
	loop %0% ;for each parameter
		{
		params.= """" %A_index% """" A_space
		}
	stringtrimright,params,params,1 ;remove trailing space
    DllCall("shell32\ShellExecuteA", uint, 0, str, "RunAs", str, A_ScriptFullPath, str, PARAMS , str, A_WorkingDir, int, 1) 
	exitapp
	return
   }

Daorc :)

guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011

this is excellent.

 

for unicode, change ShellExecuteA to ShellExecute

 

whoops another related function here:

http://www.autohotke...min-params-lib/



guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011

Button_SetShield(ButtonVar,Elevate=True) ;adapted from aw1ace's code (http://www.autohotkey.com/forum/topic50170.html)
{
static BCM_SETSHIELD=0x160C ;removed "BCM_FIRST=0x3" from aw1ace's version, because it didn't work for me with it in, and I don't really understand what it does!
GuiControlGet,ButtonHwnd,Hwnd,%ButtonVar%
SendMessage,BCM_SETSHIELD,0,Elevate,,ahk_id %ButtonHwnd%
return ErrorLevel
}


according to the links below, the BCM_FIRST=0x3 fails because thats the wrong value, and also because the BCM_SETSHIELD=0x160C already includes it

http://stackoverflow...-must-target-xp
https://svn.wsusoffl...onConstants.au3
https://github.com/A...onst_Button.ahk