小小鸭

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
'关闭计算机的模块 

Option Explicit 

Public Declare Function SetSuspendState Lib "Powrprof" (ByVal Hibernate As Boolean, ByVal ForceCritical As Boolean, ByVal DisableWakeEvent As Boolean) As Boolean 
Public Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long 

'ExitWindowsEx的参数uflags,有四个对应值,分别是: 

Public Const EWX_LOGOFF = 0 '退出(注销) 
Public Const EWX_SHUTDOWN = 1 '关机 
Public Const EWX_POWEROFF = 8 
Public Const EWX_REBOOT = 2 '重启动 
Public Const EWX_FORCE = 4 '强制关机 

Public Const TOKEN_ADJUST_PRIVILEGES = &H20 
Public Const TOKEN_QUERY = &H8 
Public Const SE_PRIVILEGE_ENABLED = &H2 
Const ANYSIZE_ARRAY = 1 

Type LUID 
lowpart As Long 
highpart As Long 
End Type 

Type LUID_AND_ATTRIBUTES 
pLuid As LUID 
Attributes As Long 
End Type 

Type TOKEN_PRIVILEGES 
PrivilegeCount As Long 
Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES 
End Type 

Declare Function GetCurrentProcess Lib "kernel32" () As Long 
Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long 
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 
Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long 

'这个函数就是用于NT关机中使用的 
Sub AdjustTokenPrivilegesForNT() 

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 

Public Sub SystemControl(Index As Integer) 
IsReset = True 
AdjustTokenPrivilegesForNT 
Select Case Index 
Case 0 
ExitWindowsEx EWX_LOGOFF, 0 
Case 1 
ExitWindowsEx EWX_POWEROFF, 0 
Case 2 
ExitWindowsEx EWX_REBOOT, 0 
Case 3 
SetSuspendState False, True, False 
Case 3 
SetSuspendState True, True, True 
End Select 
End Sub

  

posted on 2012-06-27 12:58  小小鸭  阅读(293)  评论(0编辑  收藏  举报