先说一下要调用的API:
'目的:退出系统,所有句柄以Or连接
'输入:uFlags:EWX_FORCE:可以强迫终止没有响应的进程
' EWX_LOGOFF:可以终止进程,然后注销
' EWX_SHUTDOWN:关掉系统电源
' EWX_REBOOT:重新引导系统
' dwReserved:保留,设置为0
'返回:非0即成功,0为失败!
Public Declare Function ExitWindowsEx Lib "user32" Alias "ExitWindowsEx" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
'--------------------
'以下属性皆As Long
Public Const EWX_FORCE = 4
Public Const EWX_LOGOFF = 0
Public Const EWX_REBOOT = 2
Public Const EWX_SHUTDOWN = 1
Public Const EWX_POWEROFF = 8
好,我们再来看系统:
2000以下的版本直接调用一个底层API:ExitWindowsEx()
2000以上的,由于内核变成WinTN的了,你还要给程序一个权限才行.
那么我怎样赋予程序权限呢?
Public Sub AdjustToken()
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
'----------------
'调用API函数 GetCurrentProcess 获取当前进程的句柄
hdlTokenHandle = GetCurrentProcess()
'----------------
'调用函数 OpenProcessToken 打开当前进程的访问代号,并存储在 hdlTokenHandle 中
OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle
'----------------
'获取底层权限:
LoopupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
tkp.PrivilegeCount = 1
tkp.TheLuid = tmpLuid
tkp.Attributes = SE_PRIVILEGE_ENABLED
'----------------
'为程序获取关机权限
AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded
End Sub
哈哈!这样,我们的程序就能在2000以上的环境中调用底层权限了!
为了程序更好用,我们可以这么写:
'如果操作系统不是Win 9X系列
If glngWhichWindows32 = mlngWindowsNT Then
'调用上面自定义过的函数AdjustToken
AdjustToken
End If
'调用API函数ExitWindowsEx关闭机子
Call ExitWindowsEx((EWX_SHUTDOWN Or EWX_FORCE Or EWX_POWEROFF), 0)