Shutdown the (local or remote) computer (with delay)


/ Published in: Delphi
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function TimedShutDown(Computer: string; Msg: string; Time: Word; Force: Boolean; Reboot: Boolean): Boolean;
  2. var
  3. rl: Cardinal;
  4. hToken: Cardinal;
  5. tkp: TOKEN_PRIVILEGES;
  6. begin
  7. //get user privileges to shutdown the machine, we are talking about win nt and 2k here
  8. if not OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
  9. ShowMessage('Cannot open process token.')
  10. else
  11. begin
  12. if LookupPrivilegeValue(nil, 'SeShutdownPrivilege', tkp.Privileges[0].Luid) then
  13. begin
  14. tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
  15. tkp.PrivilegeCount := 1;
  16. AdjustTokenPrivileges(hToken, False, tkp, 0, nil, rl);
  17. if GetLastError <> ERROR_SUCCESS then
  18. ShowMessage('Error adjusting process privileges.');
  19. end
  20. else
  21. ShowMessage('Cannot find privilege value.');
  22. end;
  23.  
  24. Result := InitiateSystemShutdown(PChar(Computer), PChar(Msg), Time, Force, Reboot)
  25. end;

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.