主题:如何用VB写一个定时关机的程序,,,急啊..拜托啦,,,
weios
[专家分:30] 发布于 2006-03-28 00:26:00
如何用VB写一个定时关机的程序,,,急啊..拜托啦,,,[em6][em8][em6][em1]
回复列表 (共5个回复)
沙发
tianyuan008 [专家分:14360] 发布于 2006-03-28 01:31:00
定时应该不是问题
关机可以用 API 调用
ExitWindowsEx
你去查查 本论坛的 VB API 函数库查询
板凳
weios [专家分:30] 发布于 2006-03-28 12:49:00
希望最好能提供源代码,,,,,
3 楼
tianyuan008 [专家分:14360] 发布于 2006-03-28 18:00:00
那也简单: 去 www.baidu.com 搜一下 " 程序自动关机 "
4 楼
seawaycao [专家分:3910] 发布于 2006-03-28 19:41:00
'以下代码放置在模块中
Public Const EWX_LOGOFF = 0 '`退出(注销)
Public Const EWX_SHUTDOWN = 1 '`关机
Public Const EWX_REBOOT = 2 '`重启动
Public Const EWX_POWEROFF = 8 '`物理关闭电源
Public Const EWX_FORCE = 4 '`强制关机,即不通知现在活动应用程序让其先自我关闭
Public Const TOKEN_ADJUST_PRIVILEGES = &H20
Public Const TOKEN_QUERY = &H8
Public Const SE_PRIVILEGE_ENABLED = &H2
Public Const ANYSIZE_ARRAY = 1
Type LUID
lowpart As Long
highpart As Long
End Type
Type LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End Type
Type TOKEN_PRIVILEGES
PrivilegeCount As Long
Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
End Type
'ExitWindowsEx 函数需要2个参数,可以使用一个或多个下面的标识的组合来告诉ExitWindowsEx函数希望进行关机过程。
'ExitWindowsEx的参数uflags,有四个对应值,分别是:
'EWX_FORCE 所有的进程都被强制终止
'EWX_LOGOFF 所有的进程被强制终止,并且用户退出登录(logged off)。
'EWX_POWEROFF 计算机系统被关机,并且如果支持节电特性,则计算机被物理关机。
'EWX_REBOOT 计算机系统被关机并重新启动
'EWX_SHUTDOWN 计算机被物理地安全地关机
Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Declare Function GetCurrentProcess Lib "kernel32" () As Long
Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
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
Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Sub AdjustTokenPrivilegesForNT()
On Error Resume Next
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
LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
tkp.PrivilegeCount = 1
tkp.Privileges(0).pLuid = tmpLuid
tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded
End Sub
Public Sub ShutDown()
On Error Resume Next
If IsNT = True Then AdjustTokenPrivilegesForNT '在95/98中调用没作用,取得关机句柄
ExitWindowsEx EWX_POWEROFF + EWX_FORCE, 0
End Sub
Public Sub ShutDown() '关机过程
On Error Resume Next
AdjustTokenPrivilegesForNT '在95/98中调用没作用,取得关机句柄
ExitWindowsEx EWX_POWEROFF + EWX_FORCE, 0
End Sub
'下面可以在程序中调用:
..sub.click()
ShutDown '关机
end sub
5 楼
wgx1688 [专家分:10] 发布于 2008-08-30 11:16:00
恩拿来试下~~
我来回复