注意ExitWindows的调用参数,可以分别实现关机,重新啟動以及登出當前用戶[code]Option Explicit
'这个是关机控制模块
#If VBA7 Then
Private Declare PtrSafe Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long
Private Declare PtrSafe Function GetLastError Lib "KERNEL32" () As Long
Private Declare PtrSafe Function GetCurrentProcess Lib "KERNEL32" () As Long
Private Declare PtrSafe Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare PtrSafe Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
Private Declare PtrSafe 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
Private Declare PtrSafe Sub SetLastError Lib "KERNEL32" (ByVal dwErrCode As Long)
Private Declare PtrSafe Function GetVersion Lib "KERNEL32" () As Long
#Else
Private Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long
Private Declare Function GetLastError Lib "KERNEL32" () 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 TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
Private Declare Sub SetLastError Lib "KERNEL32" (ByVal dwErrCode As Long)
Private Declare Function GetVersion Lib "KERNEL32" () As Long
#End If
'Public glngWhichWindows32 As Long
Private Type LUID
UsedPart As Long
IgnoredForNowHigh32BitPart As Long
End Type
Private Type LUID_AND_ATTRIBUTES
TheLuid As LUID
Attributes As Long
End Type
Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
TheLuid As LUID
Attributes As Long
End Type
Private Declare PtrSafe Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long
Private Declare PtrSafe Function GetLastError Lib "KERNEL32" () As Long
Private Declare PtrSafe Function GetCurrentProcess Lib "KERNEL32" () As Long
Private Declare PtrSafe Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare PtrSafe Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
Private Declare PtrSafe 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
Private Declare PtrSafe Sub SetLastError Lib "KERNEL32" (ByVal dwErrCode As Long)
Private Declare PtrSafe Function GetVersion Lib "KERNEL32" () As Long
#Else
Private Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long
Private Declare Function GetLastError Lib "KERNEL32" () 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 TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
Private Declare Sub SetLastError Lib "KERNEL32" (ByVal dwErrCode As Long)
Private Declare Function GetVersion Lib "KERNEL32" () As Long
#End If
Private Type LUID
UsedPart As Long
IgnoredForNowHigh32BitPart As Long
End Type
Private Type LUID_AND_ATTRIBUTES
TheLuid As LUID
Attributes As Long
End Type
Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
TheLuid As LUID
Attributes As Long
End Type
Public Enum ExitMode
Exit_LOGOFF = 0&
Exit_SHUTDOWN = 1&
Exit_REBOOT = 2&
Exit_FORCE = 4&
Exit_POWEROFF = 8&
Exit_FORCEIFHUNG = &H10&
Exit_RESTARTAPPS = &H40&
End Enum
Public Function ExitWindows(Optional ByVal ExitMode As ExitMode = Exit_SHUTDOWN Or Exit_FORCE) As Long