VB自动关机程序

2024年11月23日 01:45
有5个网友回答
网友(1):

完整代码
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

网友(2):

不用这么烦的
我以前也做过这
Shell
"shutdown
-S
-t
0"
就是直接关机
调试的时候当心了
会关机的
还要注意的是你这样用timer不是很准
最好是与系统时间关联下
用个format函数
可以精确到毫秒

网友(3):

Shell "shutdown -s -f -t 时间" 是定时关机!
Shell "shutdown -r -f -t 时间" 是定时重启!

x = ExitWindowsEx(EWX_SHUTDOWN, 1)
修改为:
Shell "shutdown -s -f -t " & Text1.Text & ""

网友(4):

win2000以上的机器关机,好像要取得系统的权限很麻烦的。

你如果是winxp的话,可以用下面代码一句搞定:
Private Sub Command1_Click()
Shell "cmd /c shutdown -s -t 1"
End Sub

网友(5):

前面的 Dim x As Long 而不是 Integer