Jump to content

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

Example()

Func Example()
    Local $hGUI

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

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

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

    GUISetState(@SW_SHOW)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch
    WEnd

    Exit
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.

Link to comment
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

Link to comment
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())
    Exit
EndIf

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

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

Adam

Edited by AdamUL
Link to comment
Share on other sites

  • 2 weeks later...

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...