'
'
'原创: 许仙; qq:19030300 主页:http://hot1kang1.126.com
'转载请保持信息的完整性 谢谢
' 原代码来源网络
'——————————————————————————————————
'
'Windows 95 重新开机十分简单,只要呼叫 ExitWindowsEx API 函数就可以了
'
'Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, _
' ByVal dwReserved As Long) As Long
'
' Private Enum HowExitConst
' EWX_LOGOFF = 0 ' 注销
' EWX_REBOOT = 2 ' 重开机
' EWX_SHUTDOWN = 1 ' 关机
' EWX_FORCE = 4 ' 强制结束进程关机
' End Enum
'用
'Call ExitWindowsEx(how, 0)
' ' how 等於 EWX_LOGOFF 、 EWX_REBOOT 、EWX_SHUTDOWN 、 或EWX_FORCE
'——————————————————————————————————
'在2000下以上的函数就只能注销机器~~
'
'因为 NT 关机或重新开机, 原因是 NT 比较着重安全性(Security),
'而为了让 NT 关机或重新开机, 则必须在呼叫 ExitWindowsEx 之前, 呼叫
'AdjustToken 副程式就对了?
'
'以下是小菜收集的可以关闭2000的具体原码!
'希望能给VB新手带来一点帮助!
'——————————————————————————————————
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, _
ByVal dwReserved As Long) As Long
Enum HowExitConst
EWX_FORCE = 4 ' 强制关机
EWX_LOGOFF = 0 ' 注销
EWX_REBOOT = 2 ' 重开机
EWX_SHUTDOWN = 1 ' 可关机98 在2000下关机最后出现 现在可以安全关机问题
EWX_POWEROFF = 8 '是用来关闭Windows NT/2000/XP:计算机的:
'EWX_POWEROFF:
'Shuts down the system and turns off the power. The system must support the power-off feature.
'Windows NT/2000/XP: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section.
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 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
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
'Get the LUID for shutdown privilege.
LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
tkp.PrivilegeCount = 1 ' One privilege to set
tkp.Privileges(0).pLuid = tmpLuid
tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
'Enable the shutdown privilege in the access token of this process.
AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), _
tkpNewButIgnored, lBufferNeeded
End Sub
Private Sub command1_Click()
If MsgBox("确定要注销吗?", 32 + vbOKCancel, "提醒!") = vbOK Then
AdjustToken
Call ExitWindowsEx(EWX_LOGOFF, 0)
End If
End Sub
Private Sub command2_Click()
If MsgBox("确定要关机吗?", 32 + vbOKCancel, "提醒!") = vbOK Then
AdjustToken
Call ExitWindowsEx(EWX_POWEROFF, 0) '只能注销98可正常关闭2000
Call ExitWindowsEx(EWX_SHUTDOWN, 0) '在2000下该关机有问题!出现现在可以正常关机的提示
End If
End Sub
Private Sub command3_Click()
If MsgBox("确定要重启吗?", 32 + vbOKCancel, "提醒!") = vbOK Then
AdjustToken
Call ExitWindowsEx(EWX_REBOOT, 0)
End If
End Sub