Click here to Skip to main content
15,888,803 members
Articles / Programming Languages / C#
Article

Add a UAC shield to a button when elevation is required for admin tasks

Rate me:
Please Sign up or sign in to vote.
4.55/5 (35 votes)
22 Apr 20072 min read 192K   2.5K   77   33
Add a UAC shield to a button when elevation is required for admin tasks using the API, and elevate the process if required.
Screenshot - VistaSecurity.png

Introduction

Any application that does not always need administrator privileges should not run with them by default. However, when a user wants to perform a task that requires elevation, you need to show them that this is required by displaying the Vista shield icon. When this is clicked, your application will then need to restart with administrator privileges. Interested? Then read on...

Making the VistaSecurity Class

First we need to create a VistaSecurity class. Inside it we need the SendMessage API.

C#
[DllImport("user32")]
public static extern UInt32 SendMessage
    (IntPtr hWnd, UInt32 msg, UInt32 wParam, UInt32 lParam);

internal const int BCM_FIRST = 0x1600; //Normal button
internal const int BCM_SETSHIELD = (BCM_FIRST + 0x000C); //Elevated button

First, before we add a shield, we need to know if the process is elevated or not which is easily done. To add a shield to a button, make sure that the button uses FlatStyle.System and then sends the appropriate message using the API. The important API function parameters are the handle of the button and the BCM_SETSHIELD message.

C#
static internal bool IsAdmin()
{
    WindowsIdentity id = WindowsIdentity.GetCurrent();
    WindowsPrincipal p = new WindowsPrincipal(id);
    return p.IsInRole(WindowsBuiltInRole.Administrator);
}

static internal void AddShieldToButton(Button b)
{
    b.FlatStyle = FlatStyle.System;
    SendMessage(b.Handle, BCM_SETSHIELD, 0, 0xFFFFFFFF);
}

We then need a way to elevate the process if required. We just restart the process with the "runas" Verb. The current unelevated process is then exited unless the System.ComponentModel.Win32Exception is thrown, as this indicates that the user has clicked Cancel on the UAC prompt.

C#
internal static void RestartElevated()
{
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.UseShellExecute = true;
    startInfo.WorkingDirectory = Environment.CurrentDirectory;
    startInfo.FileName = Application.ExecutablePath;
    startInfo.Verb = "runas";
    try
    {
        Process p = Process.Start(startInfo);
    }
    catch(System.ComponentModel.Win32Exception ex)
    {
        return;
    }

    Application.Exit();
}

That's it for the VistaSecurity class.

Using the code

On the form, you need the following code in your constructor after InitializeComponent which will add the shield.

C#
if (!VistaSecurity.IsAdmin())
{
    this.Text += " (Standard)"; //Unnecessary
    VistaSecurity.AddShieldToButton(buttonGetElevation); //Important
}
else
    this.Text += " (Elevated)";

When the button is clicked we need to check if you have permission to perform the required action or elevate.

C#
if (VistaSecurity.IsAdmin())
{
    DoAdminTask();
}
else
{
    VistaSecurity.RestartElevated();
}

Well, that's how to display a shield and get elevation!

Points of Interest

For the admin task in this demo, I added a file VISTA.TXT to All Users' start menu at \ProgramData\Microsoft\Windows\Start Menu\. To get rid of the file, run the demo again (admin credentials may be required!). The Try Admin Task button just tries to create the file without asking for elevation.

History

  • 22 Apr 07 - Article written
  • 24 Apr 07 - Checking of Administrator account works for non-English versions of Windows by using WindowsBuiltInRole.Administrator instead of a non-localized string. In DoAdminTask(), MessageBoxes show what has been done, more error catching added, and an Environment.SpecialFolder is used to get the path.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralPerfect Pin
almamy67-Apr-20 8:30
almamy67-Apr-20 8:30 
GeneralMy vote of 5 Pin
almamy67-Apr-20 8:09
almamy67-Apr-20 8:09 
QuestionCant get the UAC icon to a button Pin
hanonymouss1-Mar-12 4:11
hanonymouss1-Mar-12 4:11 
QuestionLicense for this Component Pin
streetsjingo30-Nov-11 10:45
streetsjingo30-Nov-11 10:45 
QuestionAwesome! + How wrong can you be? Pin
Vercas3-Oct-10 4:10
Vercas3-Oct-10 4:10 
GeneralCode Pin
Brandon Holland12-Jul-09 3:21
Brandon Holland12-Jul-09 3:21 
GeneralRestart application Pin
jammmie99924-May-09 22:55
professionaljammmie99924-May-09 22:55 
GeneralRe: Restart application Pin
McoreD30-Aug-09 15:34
McoreD30-Aug-09 15:34 
AnswerRe: Restart application Pin
Indivara28-Jan-10 14:14
professionalIndivara28-Jan-10 14:14 
Questionhow to remove it ? Pin
vishal108221-May-09 16:46
vishal108221-May-09 16:46 
AnswerRe: how to remove it ? Pin
jammmie99924-May-09 22:54
professionaljammmie99924-May-09 22:54 
AnswerRe: how to remove it ? Pin
Sky Sanders27-Dec-09 7:23
Sky Sanders27-Dec-09 7:23 
GeneralIsAdmin Function - an easier way Pin
dmex7-Jan-09 7:51
dmex7-Jan-09 7:51 
GeneralRe: IsAdmin Function - an easier way Pin
vtchris-peterson7-Jan-10 8:11
vtchris-peterson7-Jan-10 8:11 
GeneralVb.net Pin
Jorge Rocha29-Dec-08 15:05
Jorge Rocha29-Dec-08 15:05 
GeneralRe: Vb.net Pin
JumpyCODE13-Mar-09 20:55
JumpyCODE13-Mar-09 20:55 
GeneralRe: Vb.net Pin
zerosoft_uk26-Jul-11 12:14
zerosoft_uk26-Jul-11 12:14 
GeneralExcellant... and i rolled it all into a handy control :D Pin
FocusedWolf25-May-08 19:32
FocusedWolf25-May-08 19:32 
Ok so code is free without limitation... blah blah blah... that's why i pasted it here lol..

Usage: well i say this in the code comments... just add it to a project or assembly... use toolbox... drag the shield-button onto your form.

Note: if your admin, you won't see the shield icon... i assume this is the way this is supposed to work so let me know if shield should always be shown.

So ya... hook onto the Click event with stuff you want an admin to do... it'll take care of the UAC escalation... Big Grin | :-D

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Principal;
using System.Windows.Forms;

namespace StudioControls.Controls
{
    /// <summary>
    /// Makes a button that shows a shield if its application is run without admin privileges.
    /// 
    /// This control also calls up a UAC privilege escalation prompt when clicked, which is what the user expects when clicking such a "Shield" button.
    /// </summary>
    /// <example>
    /// Just drop this button in your form, and bind actions to its click event that you know would require admin privileges.
    /// If the user is an admin, there will be no shield icon, or prompt.
    /// 
    /// If you want to hide the button from an admins view, like you just want to get admin privileges, then you don't need to hook onto the click event.
    /// Just check with this controls public static member "IsAdmin()" and make this control not visible if true.
    /// 
    /// The OnClick event will trigger a UAC escalation attempt where if the user obtains admin privileges, that the program will restart. Then the second time
    /// the program is loaded, with admin privileges, then whatever is bound to the Click event will get called.
    /// 
    /// Some things to watch for:
    /// 1.  if the user gets admin priviliges, the program will restart... so your program might require saving and restoring of program state.
    ///     You can listen for this in general by hooking onto this event "Application.ApplicationExit += ..."
    ///    
    ///     However, their is no way to differentiate between UAC starting the application shutdown/restart, and a normal shutdown, so i included additional events:
    ///         EscalationStarting
    ///         EscalationCancelled
    ///         EscalationProgramRestart
    ///     
    /// 2.  When your program restarts, make sure you preserve your programs StartPosition, else it might look sloppy if it suddenly moves from where it appears in the greyed out UAC background... but then again... the taskmanager doesn't care about its StartPosition when you press "Show processes from all users" and it restarts lol. For me i just use StartPosition = CenterScreen for the affected forms.
    /// </example>
    /// <remarks>
    /// Reference: 
    ///     http://www.codeproject.com/KB/vista-security/UAC_Shield_for_Elevation.aspx, 
    ///     http://buildingsecurecode.blogspot.com/2007/06/how-to-display-uac-shield-icon-inside.html
    /// </remarks>
    public class StudioShieldButton : Button
    {
        #region Properties

        #region FlatStyle Appearance Property

        /// <summary>
        /// Determines the appearance of the control when a user moves the mouse the mouse over the control and clicks.
        /// </summary>
        [
        Category("Appearance"),
        Description("Determines the appearance of the control when a user moves the mouse the mouse over the control and clicks."),
        DefaultValue(typeof(FlatStyle), "System"),
        ReadOnly(true),
        Browsable(false)
        ]
        public new FlatStyle FlatStyle
        {
            get { return base.FlatStyle; }
        }

        #endregion

        #endregion

        #region DllImports

        private const uint BCM_SETSHIELD = 0x0000160C;

        [DllImport("user32.dll")]
        private static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        #endregion

        #region Constructor

        public StudioShieldButton()
        {
            base.FlatStyle = FlatStyle.System; // This must be FlatStyle.System for the shield to work
            // I also locked the FlatStyle property:
            //   DefaultValue(typeof(FlatStyle), "System"), 
            //   ReadOnly(true),
            //   Browsable(false) 
            // so hopefully vs.net won't try to serialize it.

            if (!HasAdminPrivileges()) // then show the shield on the button
            {
                // Send the BCM_SETSHIELD message to the button control
                SendMessage(new HandleRef(this, this.Handle), BCM_SETSHIELD, new IntPtr(0), new IntPtr(1));
            }
        }

        #endregion

        #region Members

        public static bool HasAdminPrivileges()
        {
            WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
            WindowsPrincipal windowsPrincipal = new WindowsPrincipal(windowsIdentity);
            return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
        }

        private void AttemptPrivilegeEscalation()
        {
            if (HasAdminPrivileges())
                throw new SecurityException("Already have administrator privileges.");

            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.UseShellExecute = true;
            startInfo.WorkingDirectory = Environment.CurrentDirectory;
            startInfo.FileName = Application.ExecutablePath;
            startInfo.Verb = "runas"; // will bring up the UAC run-as menu when this ProcessStartInfo is used

            #region OnUACEscalationStarting

            if (UACEscalationStarting != null)
            {
                UACEscalationStarting(this, new EventArgs());
            }

            #endregion

            try
            {
                Process.Start(startInfo);
            }
            catch (System.ComponentModel.Win32Exception) //occurs when the user has clicked Cancel on the UAC prompt.
            {
                #region OnUACEscalationCancelled

                if (UACEscalationCancelled != null)
                {
                    UACEscalationCancelled(this, new EventArgs());
                }

                #endregion

                return; // By returning, we are ignoring the user tried to get UAC priviliges but then hit cancel at the Run-As prompt.
            }
            
            // If we get here, the user successfully got admin powers (for another instance of this program), 
            // so close this low-priviliged instance of the program.

            #region OnUACEscalationAndProgramRestart

            if (UACEscalationAndProgramRestart != null)
            {
                UACEscalationAndProgramRestart(this, new EventArgs());
            }

            #endregion

            Application.Exit(); 
        }
        
        #endregion

        #region Overrided Members

        protected override void OnClick(EventArgs e)
        {
            if(HasAdminPrivileges()) // this can only be called if we're an admin.
                base.OnClick(e);

            //Their doesn't seem to be a way to achieve admin privileges without a restart, so the only other option is
            //assume the user is seeking admin privileges and help them try to.

            else
                AttemptPrivilegeEscalation();   
        }

        #endregion

        #region Events

        #region UACEscalationStarting Action

        [Category("Action")]
        [Description("This is your last chance to save your programs state, because there’s a good chance a program restart will occur, but only if the user successfully achieves admin privileges with the UAC prompt.")]
        public event EventHandler UACEscalationStarting;

        #endregion

        #region UACEscalationCancelled Action

        [Category("Action")]
        [Description("The user cancelled the UAC privilege escalation prompt. There will be no program restart.")]
        public event EventHandler UACEscalationCancelled;

        #endregion

        #region UACEscalationAndProgramRestart Action

        [Category("Action")]
        [Description("The user was successful in getting admin privileges so now this application will exit, and just previously a new instance was started.")]
        public event EventHandler UACEscalationAndProgramRestart;

        #endregion

        #endregion
    }
}

GeneralRe: Excellant... and i rolled it all into a handy control :D Pin
FocusedWolf25-May-08 19:35
FocusedWolf25-May-08 19:35 
GeneralRe: Excellant... and i rolled it all into a handy control :D Pin
FocusedWolf26-May-08 10:11
FocusedWolf26-May-08 10:11 
GeneralRe: Excellant... and i rolled it all into a handy control :D Pin
FocusedWolf30-May-08 9:40
FocusedWolf30-May-08 9:40 
GeneralRe: Excellant... and i rolled it all into a handy control :D Pin
f r i s c h19-Aug-08 1:54
f r i s c h19-Aug-08 1:54 
RantRe: Excellant... and i rolled it all into a handy control :D Pin
98z2825-Feb-10 8:48
98z2825-Feb-10 8:48 
GeneralWont detect properly except for first user in Vista Pin
leifre11-Apr-08 8:08
leifre11-Apr-08 8:08 
Question.NET 3? Pin
JimShabadoo2-Nov-07 8:34
JimShabadoo2-Nov-07 8:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.