Public Const SE_PRIVILEGE_ENABLED As Integer = &H2
Public Const TOKEN_QUERY As Integer = &H8
Public Const TOKEN_ADJUST_PRIVILEGES As Integer = &H20
Public Const SE_SHUTDOWN_NAME As String = "SeShutdownPrivilege"
Public Const EWX_LOGOFF As Integer = &H0 '注销计算机
Public Const EWX_SHUTDOWN As Integer = &H1 '关闭计算机
Public Const EWX_REBOOT As Integer = &H2 '重新启动计算机
Public Const EWX_FORCE As Integer = &H4 '关闭所有进程,注销计算机
Public Const EWX_POWEROFF As Integer = &H8
Public Const EWX_FORCEIFHUNG As Integer = &H10
Public Const SPI_GETWORKAREA = &H30
Private Type Luid
dwLowPart As Long
dwHighPart As Long
End Type
Private Type LUID_AND_ATTRIBUTES
udtLUID As Luid
dwAttributes As Long
End Type
Private Type TokPriv1Luid
Count As Integer
Luid As Luid
Attr As Integer
End Type
Private Declare Function ExitWindowsEx Lib "user32" _
(ByVal dwOptions As Long, _
ByVal dwReserved As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function OpenProcessToken Lib "advapi32" _
(ByVal ProcessHandle As Long, _
ByVal DesiredAccess As Long, _
TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32" _
Alias "LookupPrivilegeValueA" _
(ByVal lpSystemName As String, _
ByVal lpName As String, _
lpLuid As Luid) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32" _
(ByVal TokenHandle As Long, _
ByVal DisableAllPrivileges As Long, _
NewState As TokPriv1Luid, _
ByVal BufferLength As Long, _
PreviousState As Any, _
ReturnLength As Long) As Long
Private Sub DoExitWin(ByVal flg As Integer)
Dim xc As Boolean '判断语句
Dim tp As TokPriv1Luid
Dim hproc As Long
hproc = GetCurrentProcess()
'调用进程值
Dim htok As Long
htok = 0
xc = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, htok)
tp.Count = 1
tp.Attr = SE_PRIVILEGE_ENABLED
xc = LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, tp.Luid)
xc = AdjustTokenPrivileges(htok, False, tp, ByVal 0&, ByVal 0&, ByVal 0&)
xc = ExitWindowsEx(flg, 0)
End Sub
Public Sub Reboot()
Dim flg As Integer
flg = EWX_FORCE Or EWX_REBOOT
DoExitWin flg '重新启动计算机
End Sub
Public Sub PowerOff()
Dim flg As Integer
flg = EWX_FORCE Or EWX_POWEROFF
DoExitWin flg '关闭计算机
End Sub
Public Sub Logout()
Dim flg As Boolean
flg = EWX_FORCE Or EWX_LOGOFF
DoExitWin flg '注销计算机
End Sub