- 帖子
- 109
- 主題
- 2
- 精華
- 0
- 積分
- 114
- 點名
- 0
- 作業系統
- Win7 Win10
- 軟體版本
- Office 2019 WPS
- 閱讀權限
- 20
- 性別
- 男
- 來自
- 深圳
- 註冊時間
- 2013-2-2
- 最後登錄
- 2024-11-6
|
4#
發表於 2016-7-28 21:20
| 只看該作者
回復 1# spermbank
注意ExitWindows這個程式的調用參數,可以分別用以实现關閉電源、重新啟動Windows或是登出當前用戶。- #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
- 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
- '********************************************************************
- '* 這個程式用來關閉、重啟計算機或是登出當前用戶
- '********************************************************************
- Const TOKEN_ADJUST_PRIVILEGES = &H20
- Const TOKEN_QUERY = &H8
- Const SE_PRIVILEGE_ENABLED = &H2
-
- Dim hdlProcessHandle As Long
- Dim hdlTokenHandle As Long
- Dim tmpLuid As LUID
- Dim tkp As TOKEN_PRIVILEGES
- Dim tkpNewButIgnored As TOKEN_PRIVILEGES
- Dim lBufferNeeded As Long
- Dim lngVersion As Long
-
- On Error GoTo ErrorExit
- Err.Clear
- lngVersion = GetVersion()
- If ((lngVersion And &H80000000) = 0) Then
- SetLastError 0
- hdlProcessHandle = GetCurrentProcess()
- ExitWindows = GetLastError: If ExitWindows Then Exit Function
- OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle
- ExitWindows = GetLastError: If ExitWindows Then Exit Function
- LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
- ExitWindows = GetLastError: If ExitWindows Then Exit Function
- tkp.PrivilegeCount = 1
- tkp.TheLuid = tmpLuid
- tkp.Attributes = SE_PRIVILEGE_ENABLED
- AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded
- ExitWindows = GetLastError: If ExitWindows Then Exit Function
- End If
- If ExitWindowsEx(ExitMode, &HFFFF) = 0 Then ExitWindows = GetLastError
- Exit Function
- ErrorExit:
- ExitWindows = Err.Number
- End Function
- Sub PowerOff()
- ExitWindows Exit_POWEROFF Or Exit_FORCE
- End Sub
複製代碼 上一樓回復不知為何被論壇吃掉了一半的代碼…… |
|