Option Explicit
Private Declare Function GetCurrentProcess()Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function OpenProcessToken()Function OpenProcessToken Lib "advapi32" (ByVal
ProcessHandle As Long,
ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue()Function LookupPrivilegeValue Lib "advapi32" Alias
"LookupPrivilegeValueA" (
ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
Private Declare Function AdjustTokenPrivileges()Function AdjustTokenPrivileges Lib "advapi32"
(ByVal TokenHandle As Long,
ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As
Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
Const TOKEN_ADJUST_PRIVILEGES = &H20
Const TOKEN_QUERY = &H8
Const SE_PRIVILEGE_ENABLED = &H2
Private Type LUID
UsedPart As Long
IgnoredForNowHigh32BitPart As Long
End Type
Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
TheLuid As LUID
Attributes As Long
End Type
Private Declare Function ExitWindowsEx()Function ExitWindowsEx Lib "user32" (ByVal uFlags As
Long, ByVal dwReserved
As Long) As Long
Const HELP_CONTENTS = &H3&
Const EWX_WAIT = 16
Const EWX_REBOOT = 2 '重起
Const EWX_LOGOFF = 0 '注銷
Const EWX_FORCE = 4 '終止沒有響應的進程
Const EWX_SHUTDOWN = 8 '關閉電源
Private Sub Command1_Click()Sub Command1_Click()
'重新啟動計算機
ExitWindowsEx EWX_REBOOT, 0
End Sub
Private Sub Command2_Click()Sub Command2_Click()
'關閉計算機
ExitWindowsEx EWX_FORCE Or EWX_SHUTDOWN, 0
End Sub
Private Sub Command3_Click()Sub Command3_Click()
End
End Sub
Private Sub Command4_Click()Sub Command4_Click()
ExitWindowsEx EWX_LOGOFF, 0
End Sub
Private Sub Form_Load()Sub Form_Load()
Dim hProcessHandle As Long
Dim hTokenHandle As Long
Dim tmpLuid As LUID
Dim tkpNew As TOKEN_PRIVILEGES
Dim tkpPrevious As TOKEN_PRIVILEGES
Dim lBufferNeeded As Long
'獲取當前進程的一個偽句柄
hProcessHandle = GetCurrentProcess()
OpenProcessToken hProcessHandle, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY,
hTokenHandle
LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
tkpNew.PrivilegeCount = 1
tkpNew.TheLuid = tmpLuid
tkpNew.Attributes = SE_PRIVILEGE_ENABLED
lBufferNeeded = 0
'允許當前應用程序有關閉操作系統的權限
AdjustTokenPrivileges hTokenHandle, False, tkpNew, Len(tkpPrevious), tkpPrevious,
lBufferNeeded
End Sub