win98下的关机函数是什么?在线急等!
win98下的关机函数是什么?在线急等!
推荐阅读
退出操作系统可以调用windows api的exitwindowsex函数。
例子:
1、建立一个窗体,在上面放置4个按钮,按钮设置如下:
控件 控件名 caption属性
---------------------------------------------------
commandbutton cmdlogoff 注销
commandbutton cmdforcelogoff 强制注销
commandbutton cmdshutdown 关机
commandbutton cmdforceshutdown 强制关机
2、将下面的代码加入窗体中:
option explicit
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
the exitwindowsex function either logs off, shuts down, or shuts
down and restarts the system.
private declare function exitwindowsex lib "user32" _
(byval dwoptions as long, _
byval dwreserved as long) as long
the getlasterror function returns the calling threads last-error
code value. the last-error code is maintained on a per-thread basis.
multiple threads do not overwrite each others last-error code.
private declare function getlasterror lib "kernel32" () as long
private const mlngwindows95 = 0
private const mlngwindowsnt = 1
public glngwhichwindows32 as long
the getversion function returns the operating system in use.
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
the getcurrentprocess function returns a pseudohandle for the
current process.
private declare function getcurrentprocess lib "kernel32" () as long
the openprocesstoken function opens the access token associated with
a process.
private declare function openprocesstoken lib "advapi32" _
(byval processhandle as long, _
byval desiredaccess as long, _
tokenhandle as long) as long
the lookupprivilegevalue function retrieves the locally unique
identifier (luid) used on a specified system to locally represent
the specified privilege name.
private declare function lookupprivilegevalue lib "advapi32" _
alias "lookupprivilegevaluea" _
(byval lpsystemname as string, _
byval lpname as string, _
lpluid as luid) as long
the adjusttokenprivileges function enables or disables privileges
in the specified access token. enabling or disabling privileges
in an access token requires token_adjust_privileges access.
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 adjusttoken()
********************************************************************
* this procedure sets the proper privileges to allow a log off or a
* shut down to occur under windows nt.
********************************************************************
const token_adjust_privileges = &h20
const token_query = &h8
const se_privilege_enabled = &h2
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
set the error code of the last thread to zero using the
setlast error function. do this so that the getlasterror
function does not return a value other than zero for no
apparent reason.
setlasterror 0
use the getcurrentprocess function to set the hdlprocesshandle
variable.
hdlprocesshandle = getcurrentprocess()
if getlasterror <> 0 then
msgbox "getcurrentprocess error==" & getlasterror
end if
openprocesstoken hdlprocesshandle, _
(token_adjust_privileges or token_query), hdltokenhandle
if getlasterror <> 0 then
msgbox "openprocesstoken error==" & getlasterror
end if
get the luid for shutdown privilege
lookupprivilegevalue "", "seshutdownprivilege", tmpluid
if getlasterror <> 0 then
msgbox "lookupprivilegevalue error==" & getlasterror
end if
tkp.privilegecount = 1 one privilege to set
tkp.theluid = tmpluid
tkp.attributes = se_privilege_enabled
enable the shutdown privilege in the access token of this process
adjusttokenprivileges hdltokenhandle, _
false, _
tkp, _
len(tkpnewbutignored), _
tkpnewbutignored, _
lbufferneeded
if getlasterror <> 0 then
msgbox "adjusttokenprivileges error==" & getlasterror
end if
end sub
private sub cmdlogoff_click()
exitwindowsex (ewx_logoff), &hffff
msgbox "exitwindowsexs getlasterror " & getlasterror
end sub
private sub cmdforcelogoff_click()
exitwindowsex (ewx_logoff or ewx_force), &hffff
msgbox "exitwindowsexs getlasterror " & getlasterror
end sub
private sub cmdshutdown_click()
if glngwhichwindows32 = mlngwindowsnt then
adjusttoken
msgbox "post-adjusttoken getlasterror " & getlasterror
end if
exitwindowsex (ewx_shutdown), &hffff
msgbox "exitwindowsexs getlasterror " & getlasterror
end sub
private sub cmdforceshutdown_click()
if glngwhichwindows32 = mlngwindowsnt then
adjusttoken
msgbox "post-adjusttoken getlasterror " & getlasterror
end if
exitwindowsex (ewx_shutdown or ewx_force), &hffff
msgbox "exitwindowsexs getlasterror " & getlasterror
end sub
private sub form_load()
********************************************************************
* when the project starts, check the operating system used by
* calling the getversion function.
********************************************************************
dim lngversion as long
lngversion = getversion()
if ((lngversion and &h80000000) = 0) then
glngwhichwindows32 = mlngwindowsnt
msgbox "running windows nt or windows 2000"
else
glngwhichwindows32 = mlngwindows95
msgbox "running windows 95/98/me"
end if
end sub
楼上是win2000下的,win98只需要
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
exitwindowsex (ewx_shutdown), &hffff关机
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,指定下述一个或多个标志
ewx_force
强迫中止没有响应的进程
ewx_logoff
中止进程,然后注销
ewx_shutdown
关掉系统电源
ewx_reboot
重新引导系统
ewx_shutdown
关闭系统
dwreserved ----- long,保留,设为零


讨论区