2k下重起机器的源码实现,解决立即给分

解决方案 »

  1.   

    procedure AdjustToken; //获得系统NT,win2000 的关机权限
    var
      hdlProcessHandle: Cardinal;
      hdlTokenHandle: Cardinal;
      tmpLuid: Int64;
      tkp: TOKEN_PRIVILEGES;
      tkpNewButIgnored: TOKEN_PRIVILEGES;
      lBufferNeeded: Cardinal;
      Privilege: array[0..0] of _LUID_AND_ATTRIBUTES;
    begin
      hdlProcessHandle := GetCurrentProcess;
      OpenProcessToken(hdlProcessHandle,
        (TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY), hdlTokenHandle);
      LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);
      Privilege[0].Luid := tmpLuid;
      Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;
      tkp.PrivilegeCount := 1;
      tkp.Privileges[0] := Privilege[0];
      AdjustTokenPrivileges(hdlTokenHandle, False, tkp, Sizeof(tkpNewButIgnored),
        tkpNewButIgnored, lBufferNeeded);
    end;AdjustToken;
    ExitWindowsEx(EWX_REBOOT, 0)//重起
      

  2.   

    重起计算机:
    procedure Texitform.reboot_computer;
    var
      hToken: THandle;
      tkp: TOKEN_PRIVILEGES;
      ReturnLength: DWord;
    begin  if (not OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or
        TOKEN_ALL_ACCESS or TOKEN_QUERY, hToken)) then
      begin
        application.Terminate;
      end;
      LookupPrivilegeValue(nil, 'SeShutdownPrivilege', tkp.Privileges[0].Luid);
      tkp.PrivilegeCount := 1;
      tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
      ReturnLength := 0;
      AdjustTokenPrivileges(hToken, FALSE, tkp, 0, nil, ReturnLength);
      if (GetLastError() <> ERROR_SUCCESS) then
      begin
        application.Terminate;
      end;  if (not ExitWindowsEx(EWX_REBOOT, 0)) then
      begin
        application.Terminate;
      end;
    end;