VB.NET实现关机和重新启动
2024-07-10 13:01:06
供稿:网友
private declare function exitwindowsex lib "user32" (byval uflags as integer, byval dwreserved as integer) as integer
const ewx_force as short = 4
const ewx_logoff as short = 0
const ewx_reboot as short = 2
const ewx_shutdown as short = 1
dim retval as integer
' 定义esc按键
const vk_escape as short = &h1bs
private sub command1_click(byval eventsender as system.object, byval eventargs as system.eventargs) handles command1.click
if option1.checked then
' 注销当前用户
retval = exitwindowsex(ewx_force, 0)
elseif option2.checked then
' 关闭计算机
retval = exitwindowsex(ewx_shutdown, 0)
elseif option3.checked then
' 重新启动
retval = exitwindowsex(ewx_reboot, 0)
end if
end sub
private sub command2_click(byval eventsender as system.object, byval eventargs as system.eventargs) handles command2.click
me.close()
end sub
' 按esc键时,结束应用程序
private sub form1_keypress(byval eventsender as system.object, byval eventargs as system.windows.forms.keypresseventargs) handles mybase.keypress
dim keyascii as short = asc(eventargs.keychar)
if keyascii = vk_escape then
me.close()
end if
if keyascii = 0 then
eventargs.handled = true
end if
end sub
本实例通过使用exitwindowex()api函数来达到关机和重新启动的目的。在exitwindowex()函数中,参数uflags指定要进行何种操作。在表86-2中列出了参数uflags的值及其说明。
表86-2 参数uflags的值及说明
常量名
值
说明
ewx_force
4
终止所有进程,包括没有响应的进程,并注销windows
ewx_reboot
2
重新启动系统
ewx_shutdown
1
关闭系统
ewx_logoff
0
终止所有正在运行的进程,并注销windows