'再起動 Private Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long Private Const EWX_REBOOT As Long = 2 '(再起動) Private Sub comExitWin(ByVal lMode As Long) ExitWindowsEx (lMode), &HFFFF end sub用以上方法吧 还不成的话要考虑权限问题,来信索取吧[email protected]
这是我用过的代码Option ExplicitPrivate bCanShutDown As Boolean Private bCanReboot As BooleanPrivate Type LUID UsedPart As Long IgnoredForNowHigh32BitPart As Long End TypePrivate Type TOKEN_PRIVILEGES PrivilegeCount As Long TheLuid As LUID Attributes As Long End Type' Beginning of Code Private Const EWX_SHUTDOWN As Long = 1 Private Const EWX_FORCE As Long = 4 Private Const EWX_REBOOT = 2Private 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 LongPrivate 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 ' Get the LUID for shutdown privilege. LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid tkp.PrivilegeCount = 1 ' One privilege to set tkp.TheLuid = tmpLuid tkp.Attributes = SE_PRIVILEGE_ENABLED ' Enable the shutdown privilege in the access token of this ' process. AdjustTokenPrivileges hdlTokenHandle, False, tkp, _ Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeededEnd Sub'//reboot system Public Sub ForceShutdown() Call AdjustToken Call ExitWindowsEx((EWX_SHUTDOWN Or EWX_FORCE), &HFFFF) End Sub'//reboot system Public Sub ForceReboot() Call AdjustToken Call ExitWindowsEx((EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT), &HFFFF) End SubSub main() '//强行关机 Call ForceShutdown
'//重启动 Call ForceReboot
'///win98 和win2k都测试通过。
End Sub
Private Sub AllowTokenShutdown() Dim hProcessHandle As Long Dim hTokenHandle As Long Dim tmpLuid As LUID Dim tkp As TOKEN_PRIVILEGES Dim tkpNewButIgnored As TOKEN_PRIVILEGES Dim lBuffer As Long
'Get the handle to the current process hProcessHandle = GetCurrentProcess()
'Get the process token OpenProcessToken hProcessHandle, _ (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), _ hTokenHandle
'Get the LUID for shutdown privilege LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid tkp.PrivilegeCount = 1 tkp.mLuid = tmpLuid tkp.Attributes = SE_PRIVILEGE_ENABLED
'Enable shutdown access for this token AdjustTokenPrivileges hTokenHandle, _ False, _ tkp, _ Len(tkpNewButIgnored), _ tkpNewButIgnored, _ lBuffer
End Sub Public Sub Reboot() Dim rc As Long
'Give this process token access to shutdown AllowTokenShutdown
'Call the API rc = MsgBox("Are you sure to reboot?", vbOKCancel + vbDefaultButton2 + vbQuestion, "Reboot?") If rc = vbOK Then rc = ExitWindowsEx(EWX_REBOOT, 0&) End If End Sub我只贴了主要部分,那些声明之类的自己搞定就行了。 2000下顺利通过
Private Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long
Private Const EWX_REBOOT As Long = 2 '(再起動)
Private Sub comExitWin(ByVal lMode As Long) ExitWindowsEx (lMode), &HFFFF
end sub用以上方法吧
还不成的话要考虑权限问题,来信索取吧[email protected]
Private bCanReboot As BooleanPrivate Type LUID
UsedPart As Long
IgnoredForNowHigh32BitPart As Long
End TypePrivate Type TOKEN_PRIVILEGES
PrivilegeCount As Long
TheLuid As LUID
Attributes As Long
End Type' Beginning of Code
Private Const EWX_SHUTDOWN As Long = 1
Private Const EWX_FORCE As Long = 4
Private Const EWX_REBOOT = 2Private 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 LongPrivate 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 ' Get the LUID for shutdown privilege.
LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid tkp.PrivilegeCount = 1 ' One privilege to set
tkp.TheLuid = tmpLuid
tkp.Attributes = SE_PRIVILEGE_ENABLED ' Enable the shutdown privilege in the access token of this
' process.
AdjustTokenPrivileges hdlTokenHandle, False, tkp, _
Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeededEnd Sub'//reboot system
Public Sub ForceShutdown()
Call AdjustToken
Call ExitWindowsEx((EWX_SHUTDOWN Or EWX_FORCE), &HFFFF)
End Sub'//reboot system
Public Sub ForceReboot()
Call AdjustToken
Call ExitWindowsEx((EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT), &HFFFF)
End SubSub main()
'//强行关机
Call ForceShutdown
'//重启动
Call ForceReboot
'///win98 和win2k都测试通过。
End Sub
Dim hProcessHandle As Long
Dim hTokenHandle As Long
Dim tmpLuid As LUID
Dim tkp As TOKEN_PRIVILEGES
Dim tkpNewButIgnored As TOKEN_PRIVILEGES
Dim lBuffer As Long
'Get the handle to the current process
hProcessHandle = GetCurrentProcess()
'Get the process token
OpenProcessToken hProcessHandle, _
(TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), _
hTokenHandle
'Get the LUID for shutdown privilege
LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
tkp.PrivilegeCount = 1
tkp.mLuid = tmpLuid
tkp.Attributes = SE_PRIVILEGE_ENABLED
'Enable shutdown access for this token
AdjustTokenPrivileges hTokenHandle, _
False, _
tkp, _
Len(tkpNewButIgnored), _
tkpNewButIgnored, _
lBuffer
End Sub
Public Sub Reboot()
Dim rc As Long
'Give this process token access to shutdown
AllowTokenShutdown
'Call the API
rc = MsgBox("Are you sure to reboot?", vbOKCancel + vbDefaultButton2 + vbQuestion, "Reboot?")
If rc = vbOK Then
rc = ExitWindowsEx(EWX_REBOOT, 0&)
End If
End Sub我只贴了主要部分,那些声明之类的自己搞定就行了。
2000下顺利通过