首页 > 开发 > 综合 > 正文

用PB实现Windows2000下的自动关机

2024-07-21 02:10:04
字体:
来源:转载
供稿:网友

 

       在windows2000中关机与在windows98中不一样,需要首先获得权限,然后调用关机api函数,才能关机。以下是具体的步骤:

第一步: 声明结构
-----------------------------------------------------------------------

需要声明三个结:luid, luid_and_attributes, token_privileges

luid

 long  lowpart
 long  highpart


 

luid_and_attributes

 luid  pluid
 long  attributes


 

token_privileges

 long  privilegecount
 luid_and_attributes  privileges[]
 


第二步: 声明外部扩展函数
-----------------------------------------------------------------------

function ulong exitwindowsex(ulong uflags,ulong dwreserved) library "user32.dll"
function ulong getcurrentprocess() library "kernel32.dll"
function ulong lookupprivilegevalue(ref string lpsystemname,ref string lpname,ref luid lpluid) library "advapi32.dll" alias for "lookupprivilegevaluea"
function ulong adjusttokenprivileges(ulong tokenhandle,boolean disableallprivileges,ref token_privileges newstate,ulong bufferlength,ref token_privileges previousstate,ref ulong returnlength) library "advapi32.dll"
function ulong openprocesstoken(ulong processhandle,ulong desiredaccess,ref ulong tokenhandle) library "advapi32.dll"

 

第三步: 程序脚本

-----------------------------------------------------------------------

/*获得关机权限*/

string ls_lpsystemname,ls_lpname
ls_lpname='seshutdownprivilege'
ulong  lu_hdlprocesshandle,lu_hdltokenhandle,lu_lbufferneeded
luid lst_tmpluid
token_privileges lst_tkp
token_privileges lst_tkpnewbutignored
lu_hdlprocesshandle = getcurrentprocess()
openprocesstoken(lu_hdlprocesshandle,40, lu_hdltokenhandle)
lookupprivilegevalue( ls_lpsystemname, ls_lpname, lst_tmpluid)
lst_tkp.privilegecount = 1
lst_tkp.privileges[1].pluid = lst_tmpluid
lst_tkp.privileges[1].attributes = 2
adjusttokenprivileges(lu_hdltokenhandle, false, lst_tkp,16, lst_tkpnewbutignored,lu_lbufferneeded)


/*关机*/

exitwindowsex (9,0)

 
/* the end */ 

如果您在这篇文章里得到了些什么,请给作者一些鼓励:[email protected]
注册会员,创建你的web开发资料库,
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表