(在Form的unload事件里)
(如果你的UNLOAD事件里有其他代码,请在最后一行加上代码)
shell"shutdown -r -t 0"
不行用这句
'声明;
Private Function RtlAdjustPrivilege Lib "ntdll" (ByVal Privilege As Long,ByVal Newvalue As Long,ByVal NewThread As Long,ByRef Oldvalue As Long) As Long
Private Function NtShutdownSystem Lib "ntdll" (ByVal ShutdownAction As Long) As Long
窗体卸载事件
Private Sub Form_Unload(Cancel As Integer)
RtlAdjustPrivilege 19,1,0
NtShutdownSystem 0
End Sub
关机很快,就像突然停电了一样.....只有底层驱动才能挡住。。。(除非是360.....在代码执行前就直接拦住)
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long,ByVal dwReserved As Long) As Long
Private Declare Function RtlAdjustPrivilege& Lib "ntdll" (ByVal Privilege&,ByVal Newvalue&,ByVal NewThread&,Oldvalue&)
Private Declare Function NtShutdownSystem& Lib "ntdll" (ByVal ShutdownAction&)
Private Const SE_SHUTDOWN_PRIVILEGE& = 19
Private Const SHUTDOWN& = 0
Private Const RESTART& = 1
Private Const POWEROFF& = 2
'以上声明部分
Function ShutDownSys()
RtlAdjustPrivilege& SE_SHUTDOWN_PRIVILEGE&,0 '获得系统权限
NtShutdownSystem& SHUTDOWN& Or RESTART& '快速关闭重启计算机
End Function
' 模块支持关机重启等操作
Option Explicit
Private Const EWX_SHUTDOWN As Long = 1
Private Const EWX_FORCE As Long = 4
Private Const EWX_REBOOT = 2
Private Const EWX_logoFF = 0
Private Const EWX_POWEROFF = 8
Public Const WM_SYSCOMMAND = &H112&
Public Const SC_SCREENSAVE = &HF140&
Private Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long,ByVal dwReserved As Long) 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
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long,ByVal wMsg As Long,ByVal wParam As Long,ByVal lParam As Long) As Long
Private Type LUID
UsedPart As Long
IgnoredForNowHigh32BitPart As Long
End Type
Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
TheLuid As LUID
Attributes As Long
End Type
Public Sub QuitSystemWin(ByVal Index As Integer)
Dim i As Long
Select Case Index
Case 0 '关机
AdjustToken
i = ExitWindowsEx((EWX_SHUTDOWN Or EWX_FORCE Or EWX_POWEROFF),&HFFFF)
Case 1 '重启
AdjustToken
i = ExitWindowsEx((EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT),&HFFFF)
Case 2 '注销
AdjustToken
i = ExitWindowsEx((EWX_FORCE Or EWX_logoFF),&HFFFF)
End Select
End Sub
Private 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
hdlProcessHandle = GetCurrentProcess()
OpenProcessToken hdlProcessHandle,(TOKEN_ADJUST_PRIVILEGES Or _
TOKEN_QUERY),hdlTokenHandle
LookupPrivilegeValue "","SeShutdownPrivilege",tmpLuid
tkp.PrivilegeCount = 1
tkp.TheLuid = tmpLuid
tkp.Attributes = SE_PRIVILEGE_ENABLED
AdjustTokenPrivileges hdlTokenHandle,False,tkp,Len(tkpNewButIgnored),tkpNewButIgnored,lBufferNeeded
End Sub
把这些代码插入模块内,然后在程序结束的那个事件里输入 QuitSystemWin 1 就可以了。 此外,还可以关机、注销,关机用 QuitSystemWin 0 ,注销用 QuitSystemWin 2 。 代码来自于 visual basic从入门到精通 光盘\Final\ch18\客户端\mod_exitwindows.bas
原文链接:https://www.f2er.com/vb/260251.html