16,554
社区成员
发帖
与我相关
我的任务
分享
<System.Runtime.InteropServices.DllImport("kernel32.dll")> _
Private Shared Function GetCurrentProcess() As IntPtr
End Function
<System.Runtime.InteropServices.DllImport("advapi32.dll", SetLastError:=True)> _
Shared Function OpenProcessToken(ByVal ProcessHandle As IntPtr, _
ByVal DesiredAccess As Integer, _
ByRef TokenHandle As IntPtr) As Boolean
End Function
<System.Runtime.InteropServices.DllImport("advapi32.dll", SetLastError:=True, _
CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
Shared Function LookupPrivilegeValue(ByVal lpSystemName As String, _
ByVal lpName As String, _
ByRef lpLuid As Long) As Boolean
End Function
<System.Runtime.InteropServices.StructLayout( _
System.Runtime.InteropServices.LayoutKind.Sequential, Pack:=1)> _
Private Structure TOKEN_PRIVILEGES
Public PrivilegeCount As Integer
Public Luid As Long
Public Attributes As Integer
End Structure
<System.Runtime.InteropServices.DllImport("advapi32.dll", SetLastError:=True)> _
Private Shared Function AdjustTokenPrivileges(ByVal TokenHandle As IntPtr, _
ByVal DisableAllPrivileges As Boolean, _
ByRef NewState As TOKEN_PRIVILEGES, _
ByVal BufferLength As Integer, _
ByVal PreviousState As IntPtr, _
ByVal ReturnLength As IntPtr) As Boolean
End Function
Public Sub AdjustToken()
Const TOKEN_ADJUST_PRIVILEGES As Integer = &H20
Const TOKEN_QUERY As Integer = &H8
Const SE_PRIVILEGE_ENABLED As Integer = &H2
Const SE_SHUTDOWN_NAME As String = "SeShutdownPrivilege"
If Environment.OSVersion.Platform <> PlatformID.Win32NT Then
Return
End If
Dim procHandle As IntPtr = GetCurrentProcess()
Dim tokenHandle As IntPtr
OpenProcessToken(procHandle, _
TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, tokenHandle)
Dim tp As New TOKEN_PRIVILEGES()
tp.Attributes = SE_PRIVILEGE_ENABLED
tp.PrivilegeCount = 1
LookupPrivilegeValue(Nothing, SE_SHUTDOWN_NAME, tp.Luid)
AdjustTokenPrivileges(tokenHandle, False, tp, 0, IntPtr.Zero, IntPtr.Zero)
End Sub