用PB实现Windows2000下的自动关机
【打印文章】
在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 */
如果您在这篇文章里得到了些什么,请给作者一些鼓励:zaodt_ms@hotmail.com
第一步: 声明结构
-----------------------------------------------------------------------
需要声明三个结: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 */
如果您在这篇文章里得到了些什么,请给作者一些鼓励:zaodt_ms@hotmail.com
本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )
【编程爱好者论坛】