windows2000 xp 关机函数
Private Enum HowExitConstEWX_LOGOFF = 0 ''注销
EWX_SHUTDOWN = 1 ''关机
EWX_REBOOT = 2 ''重新启动
EWX_FORCE = 4 '强制关机
End Enum
Const TOKEN_ADJUST_PRIVILEGES = &H20
Const TOKEN_QUERY = &H8
Const SE_PRIVILEGE_ENABLED = &H2
Const ANYSIZE_ARRAY = 1
Private Type LUID
lowpart As Long
highpart As Long
End Type
Private Type LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End Type
Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
End Type
Private Declare Function ExitWindowsEx Lib "User32" (ByVal uFlags As Long, ByVal dwReserved 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 LUID) 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
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Dim method As HowExitConst
Dim Ydgqsql As String
Dim Ydgqdb As dao.Database
Dim Ydgqrs As dao.Recordset
Private Sub AdjustToken() '''获得系统授权函数,使程序有足够的优先权关机
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
hdlProcessHandle = GetCurrentProcess()
OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle
LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
tkp.PrivilegeCount = 1
tkp.Privileges(0).pLuid = tmpLuid
tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButignored), tkpNewButignored, lBufferNeeded
End Sub
Sub ExitWindowsGj() '''关机函数
method = EWX_SHUTDOWN
Call AdjustToken
Call ExitWindowsEx(method, 0)
End Sub
method = EWX_SHUTDOWN
DelAllSong.Enabled = True
ShellExecute 0, vbNullString, App.Path + "\end.bat", "", "", 0
Call AdjustToken '***关闭系统
Call ExitWindowsEx(method, 4)
method = EWX_REBOOT
DelAllSong.Enabled = True
ShellExecute 0, vbNullString, App.Path + "\end.bat", "", "", 0
Call AdjustToken '***重启系统
Call ExitWindowsEx(method, 2)