Jump to content
Sign in to follow this  

Prompt for UAC programatically

Recommended Posts

I am looking for a way to prompt the enduser with a UAC confirmation for running a specific task from within a script. The entire script doesn't need elevated permissions to run, just one option if chosen. (Well that's the game plan anyway).

The example script for including the UAC shield on a button indicating that the option will prompt for a UAC confirmation is all I have been able to find:

#include <GUIConstantsEx.au3>
#include <GuiButton.au3>
#include <WindowsConstants.au3>
#include <GuiMenu.au3>

Global $btn, $btn2

; Note the controlId from these buttons can NOT be read with GUICtrlRead


Func Example()
    Local $hGUI

    $hGUI = GUICreate("Buttons", 400, 400)

    $btn = _GUICtrlButton_Create($hGUI, "Button1", 10, 10, 90, 30)

    $btn2 = _GUICtrlButton_Create($hGUI, "Button2", 10, 60, 90, 30, $BS_SPLITBUTTON)


    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE

EndFunc   ;==>Example

But what would be necessary to actually make the UAC confirmation occur?

#RequireAdmin would force the whole scrpt to prompt on each run for elevated permissions. I just want to place this on a button option only.

Otherwise, make the function into it's own program with the #RequireAdmin...which seems a bit much.

I have done quite a bit of searching, but haven't found how to write the code just for the one button to prompt for UAC.

Thank you for any help.

Share this post

Link to post
Share on other sites

You need two separate scripts/exes to do this, the second one should have the #RequireAdmin directive in it so that when it is run it asks for permission.

You can use FileInstall to package your second script with the first one, you could also compile the second one to an A3X instead of an exe to make it smaller and run it with the first script.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post

Link to post
Share on other sites

Thank  you, BrewManNH, for your quick response! That has been the only way I was able to do it in the past, but it just seemed kind of kludgy.

I have never tried compiling into an A3X before, so thank you for that suggestion too.

Share this post

Link to post
Share on other sites

You could also use re-execution with ShellExecute with the "runas" verb.  Then use the IsAdmin function at the top of the script, with the section of code that needs to be with full admin rights.  Here is a very simple example.  

;Run Admin part.  Put at top of script.  
If IsAdmin() Then   
    MsgBox(0, "Is Admin", IsAdmin())

MsgBox(0, "Is NOT Admin", IsAdmin())

;Run with UAC Elevation
If @Compiled Then 
    ShellExecute(@AutoItExe, '', '', 'runas')
    ShellExecute(@AutoItExe, '"' & @ScriptFullPath & '"', '', 'runas')


Edited by AdamUL

Share this post

Link to post
Share on other sites

Hello AdamUL,

Thank you so much for your reply! I'm sorry I didn't see it earlier. I appreciate it and will look into implementing your suggestion!

Thanks again!

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...