怎样用VB做个关机程序?

polly_110 2003-11-01 11:13:28
想用VB做个延时关机的程序,当延时的时间到后自动关机,刚学会做倒计时的程序,当时间到后需要调用哪些API函数或程序才能关机?怎样调用?
...全文
166 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
polly_110 2003-11-06
  • 打赏
  • 举报
回复
谢谢马可,已经OK啦!还有 pigpag(噼里啪啦) ,你的意见是对的, 应该改成:private.
taosihai1only 2003-11-01
  • 打赏
  • 举报
回复
Private Const EWX_LogOff As Long = 0
Private Const EWX_SHUTDOWN As Long = 1
Private Const EWX_REBOOT As Long = 2
Private Const EWX_FORCE As Long = 4
Private Const EWX_POWEROFF As Long = 8
Private Declare Function ExitWindowsEx Lib "user32" _
(ByVal dwOptions As Long, _
ByVal dwReserved As Long) As Long
Private Declare Function GetLastError Lib "kernel32" () As Long

Private Const mlngWindows95 = 0
Private Const mlngWindowsNT = 1

Public glngWhichWindows32 As Long

Private Declare Function GetVersion Lib "kernel32" () As Long

Private Type LUID
UsedPart As Long
IgnoredForNowHigh32BitPart As Long
End Type

Private Type LUID_AND_ATTRIBUTES
TheLuid As LUID
Attributes As Long
End Type

Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
TheLuid As LUID
Attributes As Long
End Type
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

Private Declare Sub SetLastError Lib "kernel32" _
(ByVal dwErrCode As Long)

Private Sub cmdShutdown_Click()
If glngWhichWindows32 = mlngWindowsNT Then
AdjustToken
MsgBox "Post-AdjustToken GetLastError " & GetLastError
End If

private sub timer_time
call cmdshutdown
endif

ExitWindowsEx (EWX_SHUTDOWN), &HFFFF
MsgBox "ExitWindowsEx's GetLastError " & GetLastError
End Sub
efsnake 2003-11-01
  • 打赏
  • 举报
回复
同意楼上的方法,我也想不出别的方法了!
pandengzhe 2003-11-01
  • 打赏
  • 举报
回复
嗯,好像没别的方法了
txlicenhe 2003-11-01
  • 打赏
  • 举报
回复
Const EWX_LOGOFF = 0
Const EWX_SHUTDOWN = 1
Const EWX_REBOOT = 2
Const EWX_FORCE = 4
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Private Sub Form_Load()
msg = MsgBox("This program is going to reboot your computer. Press OK to continue or Cancel to stop.", vbCritical + vbOKCancel + 256, App.Title)
If msg = vbCancel Then End
'reboot the computer
ret& = ExitWindowsEx(EWX_FORCE Or EWX_REBOOT, 0)
End Sub



有二种方法可以实现:

1.用shell函数就可以实现:

Shell "rundll.exe user.exe,exitwindowsexec", vbHide   '重起
Shell "rundll.exe user.exe,exitwindows", vbHide   '关机

2.用API函数,exitwidnowsEx 函数

【VB声明】
Private Declare Function ExitWindowsEx Lib "user32" Alias "ExitWindowsEx" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
【说明】
退出windows,并用特定的选项重新启动

【返回值】
Long,非零表示成功,零表示失败。会设置GetLastError

【备注】
这个函数调用后会立刻返回,系统关闭过程是在后台进行的。注意先中止自己的应用程序,使关闭过程更显平顺。当然,您的进程必须有足够的优先权,否则也不能执行这种操作

【参数表】
uFlags --------- Long,指定下述一个或多个标志(用OR运算符合并到一起)
EWX_FORCE
强迫中止没有响应的进程
EWX_LOGOFF
中止进程,然后注销
EWX_SHUTDOWN
关掉系统电源(如果可能的话,ATX电源就可以)
EWX_REBOOT
重新引导系统
EWX_SHUTDOWN
关闭系统

dwReserved ----- Long,保留,设为零



/************** 定时关机

'模块定义
Enum HowExitConst
EWX_FORCE = 4 ' 强制关机
EWX_LOGOFF = 0 ' 登出
EWX_REBOOT = 2 ' 重开机
EWX_SHUTDOWN = 1 ' 关机
EWX_POWEROFF = 8
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

Public Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias _
"LookupPrivilegeValueA" (ByVal lpSystemName As String, _
ByVal lpName As String, lpLuid As LUID) As Long
Private 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
Private Declare Function OpenProcessToken Lib "advapi32.dll" _
(ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, _
TokenHandle As Long) As Long

Public Sub AdjustToken()
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
tkp.PrivilegeCount = 1 ' One privilege to set
tkp.Privileges(0).pLuid = tmpLuid
tkp.Privileges(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 Sub Timer1_Timer()
If Hour(Now) = i Then
'win2000
AdjustToken
ExitWindowsEx EWX_POWEROFF, 0
'win98
ExitWindowsEx EWX_SHUTDOWN, 0
End If
End Sub
pigpag 2003-11-01
  • 打赏
  • 举报
回复
Public Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long

如果放在窗体模块里,只能声明为Private
shortppsy 2003-11-01
  • 打赏
  • 举报
回复
Public Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long,_
ByVal dwReserved As Long) As Long
一行最后加下划线
polly_110 2003-11-01
  • 打赏
  • 举报
回复
TO马可:
Public Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
上面的这一行怎么编译不能通过啊?
polly_110 2003-11-01
  • 打赏
  • 举报
回复
TO 马可,
Private Sub Form_Load()
msg = MsgBox("This program is going to reboot your computer. Press OK to continue or Cancel to stop.", vbCritical + vbOKCancel + 256, App.Title)
If msg = vbCancel Then End
'reboot the computer
ret& = ExitWindowsEx(EWX_FORCE Or EWX_REBOOT, 0)
End Sub
这段代码是可以关机,但是有点不足之处就是关机前还在问一下你是否确定关机, 我不想要这个确认的过程,比如,有时睡着了, 就想让它直接关机.
polly_110 2003-11-01
  • 打赏
  • 举报
回复
谢谢各位大侠, 小弟今晚试一试, 明天再来结贴吧.
kmzs 2003-11-01
  • 打赏
  • 举报
回复
Shell "rundll.exe user.exe,exitwindowsexec", vbHide   '重起
Shell "rundll.exe user.exe,exitwindows", vbHide   '关机
since1990 2003-11-01
  • 打赏
  • 举报
回复
up
lihonggen0 2003-11-01
  • 打赏
  • 举报
回复


对于windows 98 关闭计算机:

Const EWX_LOGOFF = 0
Const EWX_SHUTDOWN = 1
Const EWX_REBOOT = 2
Const EWX_FORCE = 4
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Private Sub Form_Load()
ret& = ExitWindowsEx(EWX_FORCE Or EWX_REBOOT, 0)
End Sub











Windows 2000 、XP 下的关闭计算机

http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=18622

http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=166814
fulongbiao 2003-11-01
  • 打赏
  • 举报
回复
api调用,同意楼上几位老鸟的做法。
ExitWindowsEx()
danpianji2002 2003-11-01
  • 打赏
  • 举报
回复
同意,取得权限后关机

7,771

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧