在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