用PB实现Windows2000下的自动关机

    技术2022-05-11  125

     

           在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_lpNamels_lpname='SeShutdownPrivilege'ulong  lu_hdlProcessHandle,lu_hdlTokenHandle,lu_lBufferNeededluid lst_tmpLuidTOKEN_PRIVILEGES lst_tkpTOKEN_PRIVILEGES lst_tkpNewButIgnoredlu_hdlProcessHandle = GetCurrentProcess()OpenProcessToken(lu_hdlProcessHandle,40, lu_hdlTokenHandle)LookupPrivilegeValue( ls_lpSystemName, ls_lpname, lst_tmpLuid)lst_tkp.PrivilegeCount = 1lst_tkp.Privileges[1].pLuid = lst_tmpLuidlst_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


    最新回复(0)