Imports System.Collections.Generic Imports System.Text Imports System.Runtime.InteropServices Imports System.Diagnostics Imports System.Windows.Forms Imports System.Security.Principal Namespace hackman3vilGuy.CodeProject.VistaSecurity.ElevateWithButton Public NotInheritable Class VistaSecurity Private Sub New() End Sub _ Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As UInt32, ByVal wParam As UInt32, ByVal lParam As UInt32) As UInt32 End Function Friend Const BCM_FIRST As Integer = &H1600 Friend Const BCM_SETSHIELD As Integer = (BCM_FIRST + &HC) Friend Shared Function IsVistaOrHigher() As Boolean Return Environment.OSVersion.Version.Major < 6 End Function ''' ''' Checks if the process is elevated ''' ''' If is elevated Friend Shared Function IsAdmin() As Boolean Dim id As WindowsIdentity = WindowsIdentity.GetCurrent() Dim p As New WindowsPrincipal(id) Return p.IsInRole(WindowsBuiltInRole.Administrator) End Function ''' ''' Add a shield icon to a button ''' ''' The button Friend Shared Sub AddShieldToButton(ByVal b As Button) b.FlatStyle = FlatStyle.System SendMessage(b.Handle, BCM_SETSHIELD, 0, &HFFFFFFFFUI) End Sub ''' ''' Restart the current process with administrator credentials ''' Friend Shared Sub RestartElevated() Dim startInfo As New ProcessStartInfo() startInfo.UseShellExecute = True startInfo.WorkingDirectory = Environment.CurrentDirectory startInfo.FileName = Application.ExecutablePath startInfo.Verb = "runas" Try Dim p As Process = Process.Start(startInfo) Catch ex As System.ComponentModel.Win32Exception 'If cancelled, do nothing Return End Try Application.[Exit]() End Sub End Class End Namespace