重启、注销、关机windows系统的正确写法(VB代码)
Attribute VB_Name = "System_Shutdown_Module"
Option Explicit
Private Const EWX_LOGOFF = 0
Private Const EWX_SHUTDOWN = 1
Private Const EWX_REBOOT = 2
Private Const EWX_FORCE = 4
Private Const EWX_POWEROFF = 8
Private Const TOKEN_QUERY = &H8
Private Const TOKEN_ADJUST_PRIVILEGES = &H20
Private Const SE_SHUTDOWN_NAME = "SeShutdownPrivilege" Private Const SE_PRIVILEGE_ENABLED = &H2
Private Const ANYSIZE_ARRAY = 1
Private Type LARGE_INTEGER
lowpart As Long
highpart As Long
End Type
Private Type LUID_AND_ATTRIBUTES
'pLuid As Luid
pLuid As LARGE_INTEGER
Attributes As Long
End Type
Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
End Type
Private Declare Function LockWorkStation Lib "user32.dll" () As Long Private Declare Function ExitWindows Lib "user32" (ByVal dwReserved As Long, ByVal
uReturnCode As Long) As Long
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal
dwReserved As Long) As Long
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle
As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias
"LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String,
lpLuid As LARGE_INTEGER) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (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
'[[[[[[[[[[ 《重启、注销、关机windows系统的正确写法》 Code by EnVon QQ:407542585 ]]]]]]]]]]]]]]
'锁定计算机
Public Function 锁定计算机() As Boolean
If LockWorkStation Then
锁定计算机 = True
Else
锁定计算机 = False
End If
End Function
'///////////////////////////////////////////////////////////////////////////////
///////
'正常注销计算机
Public Function 正常注销计算机() As Boolean
If ExitWindowsEx(EWX_LOGOFF, 0) Then
正常注销计算机 = True
Else
正常注销计算机 = False
End If
End Function
'强制注销计算机
Public Function 强制注销计算机() As Boolean
If ExitWindowsEx(EWX_FORCE, 0) Then
强制注销计算机 = True
Else
强制注销计算机 = False
End If
End Function
'///////////////////////////////////////////////////////////////////////////////
///////
'正常关闭计算机
Public Function 正常关闭计算机() As Boolean
Dim retB As Boolean
Dim hProc As Long
Dim tkp As TOKEN_PRIVILEGES, tkpLen As Long
Dim outTok As TOKEN_PRIVILEGES, outTokLen As Long
retB = False
If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hProc) Then
If LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, tkp.Privileges(0).pLuid) Then
tkp.PrivilegeCount = 1
tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
tkpLen = Len(tkp)
If AdjustTokenPrivileges(hProc, False, tkp, tkpLen, outTok, outTokLen)
Then
If ExitWindowsEx(EWX_SHUTDOWN, 0) Then retB = True
End If
End If
End If
正常关闭计算机 = retB
End Function
'强制关闭计算机
Public Function 强制关闭计算机() As Boolean
Dim retB As Boolean
Dim hProc As Long
Dim tkp As TOKEN_PRIVILEGES, tkpLen As Long
Dim outTok As TOKEN_PRIVILEGES, outTokLen As Long
retB = False
If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hProc) Then
If LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, tkp.Privileges(0).pLuid) Then
tkp.PrivilegeCount = 1
tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
tkpLen = Len(tkp)
If AdjustTokenPrivileges(hProc, False, tkp, tkpLen, outTok, outTokLen)
Then
If ExitWindowsEx(EWX_SHUTDOWN Or EWX_FORCE, 0) Then retB = True
End If
End If
End If
强制关闭计算机 = retB
End Function
'///////////////////////////////////////////////////////////////////////////////
///////
'正常关闭电源
Public Function 正常关闭电源() As Boolean
Dim retB As Boolean
Dim hProc As Long
Dim tkp As TOKEN_PRIVILEGES, tkpLen As Long
Dim outTok As TOKEN_PRIVILEGES, outTokLen As Long
retB = False
If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hProc) Then
If LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, tkp.Privileges(0).pLuid) Then
tkp.PrivilegeCount = 1
tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
tkpLen = Len(tkp)
If AdjustTokenPrivileges(hProc, False, tkp, tkpLen, outTok, outTokLen)
Then
If ExitWindowsEx(EWX_POWEROFF, 0) Then retB = True
End If
End If
End If
正常关闭电源 = retB
End Function
'强制关闭电源
Public Function 强制关闭电源() As Boolean
Dim retB As Boolean
Dim hProc As Long
Dim tkp As TOKEN_PRIVILEGES, tkpLen As Long
Dim outTok As TOKEN_PRIVILEGES, outTokLen As Long
retB = False
If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hProc) Then
If LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, tkp.Privileges(0).pLuid) Then
tkp.PrivilegeCount = 1
tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
tkpLen = Len(tkp)
If AdjustTokenPrivileges(hProc, False, tkp, tkpLen, outTok, outTokLen)
Then
If ExitWindowsEx(EWX_POWEROFF Or EWX_FORCE, 0) Then retB = True
End If
End If
End If
强制关闭电源 = retB
End Function
'///////////////////////////////////////////////////////////////////////////////
///////
'正常重启计算机
Public Function 正常重启计算机() As Boolean
Dim retB As Boolean
Dim hProc As Long
Dim tkp As TOKEN_PRIVILEGES, tkpLen As Long
Dim outTok As TOKEN_PRIVILEGES, outTokLen As Long
retB = False
If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hProc) Then
If LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, tkp.Privileges(0).pLuid) Then
tkp.PrivilegeCount = 1
tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
tkpLen = Len(tkp)
If AdjustTokenPrivileges(hProc, False, tkp, tkpLen, outTok, outTokLen)
Then
If ExitWindowsEx(EWX_REBOOT, 0) Then retB = True
End If
End If
End If
正常重启计算机 = retB
End Function
'强制重启计算机
Public Function 强制重启计算机() As Boolean
Dim retB As Boolean
Dim hProc As Long
Dim tkp As TOKEN_PRIVILEGES, tkpLen As Long
Dim outTok As TOKEN_PRIVILEGES, outTokLen As Long
retB = False
If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hProc) Then
If LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, tkp.Privileges(0).pLuid) Then
tkp.PrivilegeCount = 1
tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
tkpLen = Len(tkp)
If AdjustTokenPrivileges(hProc, False, tkp, tkpLen, outTok, outTokLen)
Then
If ExitWindowsEx(EWX_REBOOT Or EWX_FORCE, 0) Then retB = True
End If
End If
End If
强制重启计算机 = retB
End Function