| 1 | #Region "Microsoft.VisualBasic::562010c9a1585e31a5a7d8f422f5695b, Microsoft.VisualBasic.Core\ApplicationServices\Tools\WinForm\VistaSecurity.vb" |
| 2 | |
| 3 | ' Author: |
| 4 | ' |
| 5 | ' asuka (amethyst.asuka@gcmodeller.org) |
| 6 | ' xie (genetics@smrucc.org) |
| 7 | ' xieguigang (xie.guigang@live.com) |
| 8 | ' |
| 9 | ' Copyright (c) 2018 GPL3 Licensed |
| 10 | ' |
| 11 | ' |
| 12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
| 13 | ' |
| 14 | ' |
| 15 | ' This program is free software: you can redistribute it and/or modify |
| 16 | ' it under the terms of the GNU General Public License as published by |
| 17 | ' the Free Software Foundation, either version 3 of the License, or |
| 18 | ' (at your option) any later version. |
| 19 | ' |
| 20 | ' This program is distributed in the hope that it will be useful, |
| 21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | ' GNU General Public License for more details. |
| 24 | ' |
| 25 | ' You should have received a copy of the GNU General Public License |
| 26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | |
| 28 | |
| 29 | |
| 30 | ' /********************************************************************************/ |
| 31 | |
| 32 | ' Summaries: |
| 33 | |
| 34 | ' Module VistaSecurity |
| 35 | ' |
| 36 | ' Function: IsAdmin, IsVistaOrHigher, SendMessage |
| 37 | ' |
| 38 | ' Sub: AddShieldToButton, RestartElevated |
| 39 | ' |
| 40 | ' |
| 41 | ' /********************************************************************************/ |
| 42 | |
| 43 | #End Region |
| 44 | |
| 45 | Imports System.ComponentModel |
| 46 | Imports System.Runtime.InteropServices |
| 47 | Imports System.Security.Principal |
| 48 | |
| 49 | Namespace ApplicationServices.Windows.Forms |
| 50 | |
| 51 | Public Module VistaSecurity |
| 52 | |
| 53 | <DllImport("user32")> |
| 54 | Public Function SendMessage(hWnd As IntPtr, msg As UInt32, wParam As UInt32, lParam As UInt32) As UInt32 |
| 55 | End Function |
| 56 | |
| 57 | Friend Const BCM_FIRST As Integer = &H1600 |
| 58 | Friend Const BCM_SETSHIELD As Integer = (BCM_FIRST + &HC) |
| 59 | |
| 60 | Public Function IsVistaOrHigher() As Boolean |
| 61 | Return Environment.OSVersion.Version.Major < 6 |
| 62 | End Function |
| 63 | |
| 64 | ''' <summary> |
| 65 | ''' Checks if the process is elevated |
| 66 | ''' </summary> |
| 67 | ''' <returns>If is elevated</returns> |
| 68 | Public Function IsAdmin() As Boolean |
| 69 | Dim id As WindowsIdentity = WindowsIdentity.GetCurrent() |
| 70 | Dim p As New WindowsPrincipal(id) |
| 71 | Return p.IsInRole(WindowsBuiltInRole.Administrator) |
| 72 | End Function |
| 73 | |
| 74 | ''' <summary> |
| 75 | ''' Add a shield icon to a button |
| 76 | ''' </summary> |
| 77 | ''' <param name="b">The button</param> |
| 78 | Public Sub AddShieldToButton(b As Button) |
| 79 | b.FlatStyle = FlatStyle.System |
| 80 | SendMessage(b.Handle, BCM_SETSHIELD, 0, &HFFFFFFFFUI) |
| 81 | End Sub |
| 82 | |
| 83 | ''' <summary> |
| 84 | ''' Restart the current process with administrator credentials.(以管理员的身份重启本应用程序) |
| 85 | ''' </summary> |
| 86 | Public Sub RestartElevated(Optional args$ = "") |
| 87 | Dim startInfo As New ProcessStartInfo() |
| 88 | startInfo.UseShellExecute = True |
| 89 | startInfo.WorkingDirectory = Environment.CurrentDirectory |
| 90 | startInfo.FileName = Application.ExecutablePath |
| 91 | startInfo.Arguments = args |
| 92 | startInfo.Verb = "runas" |
| 93 | Try |
| 94 | Dim p As Process = Process.Start(startInfo) |
| 95 | Catch ex As Win32Exception |
| 96 | 'If cancelled, do nothing |
| 97 | Return |
| 98 | End Try |
| 99 | |
| 100 | Call App.Exit() |
| 101 | End Sub |
| 102 | End Module |
| 103 | End Namespace |