VB中能强制关机和重启动的代码是什么

如题... 如题 展开
 我来答
mouldy1n
2010-06-21 · TA获得超过517个赞
知道小有建树答主
回答量:236
采纳率:100%
帮助的人:228万
展开全部
Enum HowExitConst
EWX_FORCE = 4 ' 强制关机
EWX_LOGOFF = 0 ' 登出
EWX_REBOOT = 2 ' 重开机
EWX_SHUTDOWN = 1 ' 关机
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 ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
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

'NT比较着重安全性(Security),而为了让NT关机或重新开机,则必须在呼叫ExitWindowsEx之前,呼叫AdjustToken副程式。
'在Windows95/98底下呼叫了AdjustToken也没关系,因为Windows95/98并不会理会安全性的设定,只有win2000以上才有用。

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

'用法:
AdjustToken
Call ExitWindowsEx(EWX_REBOOT, 0) 'EWX_REBOOT可换为EWX_LOGOFF、EWX_REBOOT、EWX_SHUTDOWN、或EWX_FORCE
无敌闹闹i
2010-06-20 · TA获得超过328个赞
知道答主
回答量:137
采纳率:0%
帮助的人:81.6万
展开全部
shell "shutdown -f -s -t 0" '关机
shell "shutdown -f -r -t 0" '重启
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友301d0e3
2010-06-20 · TA获得超过196个赞
知道答主
回答量:113
采纳率:0%
帮助的人:0
展开全部
shell "shutdown -s -t 0"
shell "shutdown -r -t 0"
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式