VB自动关机程序

PrivateDeclareFunctionExitWindowsExLib"user32"(ByValuFlagsAsLong,ByValdwReservedAsLon... Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Dim x As Integer

Private Sub Form_Load()
Text1.Text = ""
Timer1.Interval = 300
End Sub

Private Sub Timer1_Timer()
Text1.Text = Time
If Text1.Text = "10:30:50" Then
x = ExitWindowsEx(EWX_SHUTDOWN, 1)
End If
End Sub
这个调取系统时间!当时间指定到我设置的时间时系统自动关机!可是这段代码老是注销系统!请大虾帮我看看是哪错了!谢谢
我按照上述几位朋友的方法试了!可是还是注销!
展开
 我来答
sihai163
推荐于2016-05-22 · TA获得超过160个赞
知道小有建树答主
回答量:469
采纳率:0%
帮助的人:376万
展开全部
完整代码
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
Const TOKEN_ADJUST_PRIVILEGES = &H20
Const TOKEN_QUERY = &H8
Const SE_PRIVILEGE_ENABLED = &H2
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
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Const HELP_CONTENTS = &H3&
Const EWX_LOGOFF = 0 '注销
Const EWX_SHUTDOWN = 1 '待机
Const EWX_REBOOT = 2 '重新启动
Const EWX_WAIT = 16
Const EWX_FORCE = 4 '终止没有响应的进程
Const EWX_POWEROFF = 8 '关闭电源
Private Sub Form_Load()
Dim hProcessHandle As Long
Dim hTokenHandle As Long
Dim tmpLuid As LUID
Dim tkpNew As TOKEN_PRIVILEGES
Dim tkpPrevious As TOKEN_PRIVILEGES
Dim lBufferNeeded As Long
'获取当前进程的一个伪句柄
hProcessHandle = GetCurrentProcess()
OpenProcessToken hProcessHandle, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hTokenHandle
LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
tkpNew.PrivilegeCount = 1
tkpNew.TheLuid = tmpLuid
tkpNew.Attributes = SE_PRIVILEGE_ENABLED
lBufferNeeded = 0
'允许当前应用程序有关闭操作系统的权限
AdjustTokenPrivileges hTokenHandle, False, tkpNew, Len(tkpPrevious), tkpPrevious, lBufferNeeded
end sub
Private Sub Command1_Click()
ExitWindowsEx EWX_FORCE Or EWX_POWEROFF, 0
End Sub
修气骆花
2019-06-21 · TA获得超过3856个赞
知道大有可为答主
回答量:3137
采纳率:32%
帮助的人:193万
展开全部
不用这么烦的
我以前也做过这
Shell
"shutdown
-S
-t
0"
就是直接关机
调试的时候当心了
会关机的
还要注意的是你这样用timer不是很准
最好是与系统时间关联下
用个format函数
可以精确到毫秒
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友2ca1c0f24
2008-02-14 · TA获得超过2036个赞
知道大有可为答主
回答量:4364
采纳率:0%
帮助的人:3428万
展开全部
Shell "shutdown -s -f -t 时间" 是定时关机!
Shell "shutdown -r -f -t 时间" 是定时重启!

x = ExitWindowsEx(EWX_SHUTDOWN, 1)
修改为:
Shell "shutdown -s -f -t " & Text1.Text & ""
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
jyh_jack
2008-02-14 · TA获得超过4195个赞
知道大有可为答主
回答量:2080
采纳率:100%
帮助的人:2832万
展开全部
win2000以上的机器关机,好像要取得系统的权限很麻烦的。

你如果是winxp的话,可以用下面代码一句搞定:
Private Sub Command1_Click()
Shell "cmd /c shutdown -s -t 1"
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
msy0613
2008-02-14 · TA获得超过2.6万个赞
知道大有可为答主
回答量:6546
采纳率:0%
帮助的人:2918万
展开全部
前面的 Dim x As Long 而不是 Integer
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式