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
这个调取系统时间!当时间指定到我设置的时间时系统自动关机!可是这段代码老是注销系统!请大虾帮我看看是哪错了!谢谢
我按照上述几位朋友的方法试了!可是还是注销! 展开
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
这个调取系统时间!当时间指定到我设置的时间时系统自动关机!可是这段代码老是注销系统!请大虾帮我看看是哪错了!谢谢
我按照上述几位朋友的方法试了!可是还是注销! 展开
5个回答
展开全部
完整代码
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
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
展开全部
不用这么烦的
我以前也做过这
Shell
"shutdown
-S
-t
0"
就是直接关机
调试的时候当心了
会关机的
还要注意的是你这样用timer不是很准
最好是与系统时间关联下
用个format函数
可以精确到毫秒
我以前也做过这
Shell
"shutdown
-S
-t
0"
就是直接关机
调试的时候当心了
会关机的
还要注意的是你这样用timer不是很准
最好是与系统时间关联下
用个format函数
可以精确到毫秒
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Shell "shutdown -s -f -t 时间" 是定时关机!
Shell "shutdown -r -f -t 时间" 是定时重启!
x = ExitWindowsEx(EWX_SHUTDOWN, 1)
修改为:
Shell "shutdown -s -f -t " & Text1.Text & ""
Shell "shutdown -r -f -t 时间" 是定时重启!
x = ExitWindowsEx(EWX_SHUTDOWN, 1)
修改为:
Shell "shutdown -s -f -t " & Text1.Text & ""
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
win2000以上的机器关机,好像要取得系统的权限很麻烦的。
你如果是winxp的话,可以用下面代码一句搞定:
Private Sub Command1_Click()
Shell "cmd /c shutdown -s -t 1"
End Sub
你如果是winxp的话,可以用下面代码一句搞定:
Private Sub Command1_Click()
Shell "cmd /c shutdown -s -t 1"
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
前面的 Dim x As Long 而不是 Integer
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询