AddPower management procedure (shutdown,log off, screensaver, etc)
  1. //I guess it will works in all windows
  2. function PowerMng(Action:String;Force:Boolean):boolean;
  3. var rl: Cardinal;
  4.     hToken: Cardinal;
  5.     tkp: TOKEN_PRIVILEGES;
  6. begin
  7.  if Win32Platform = VER_PLATFORM_WIN32_NT then
  8.   begin
  9. //Get access to windows privilege
  10.        OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken);
  11.        LookupPrivilegeValue(nil, 'SeShutdownPrivilege', tkp.Privileges[0].Luid);
  12.        tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;;
  13.        tkp.PrivilegeCount := 1;
  14.        AdjustTokenPrivileges(hToken, False, tkp, 0, nil, rl);
  15. //Shutdown Windows
  16.               if (Action='1') and (Force=false) then
  17.                begin
  18.                     ExitWindowsEx(EWX_SHUTDOWN, 0)
  19.                      end else
  20.               if (Action='1') and (Force=true) then
  21.                begin
  22.                     (ExitWindowsEx(EWX_SHUTDOWN or EWX_FORCE, 0));
  23.                       end;
  24. //Restart/Reboot Windows
  25.               if (Action='2') and (Force=false) then
  26.                begin
  27.                     ExitWindowsEx(EWX_REBOOT, 0)
  28.                      end else
  29.               if (Action='2') and (Force=true) then
  30.                begin
  31.                     (ExitWindowsEx(EWX_REBOOT or EWX_FORCE, 0));
  32.                      end;
  33. //Log Off Windows
  34.               if (Action='3') and (Force=false) then
  35.                begin
  36.                     ExitWindowsEx(EWX_LOGOFF, 0)
  37.                      end else
  38.               if (Action='3') and (Force=true) then
  39.                begin
  40.                     (ExitWindowsEx(EWX_LOGOFF or EWX_FORCE, 0));
  41.                      end;
  42. //Turn off monitor
  43.               if (Action='4') and (Force=true) then
  44.                begin
  45.                     SendMessage(Application.Handle,WM_SYSCOMMAND,SC_MONITORPOWER,2);
  46.                      end else
  47.               if (Action='4') and (Force=true) then //Turn on monitor
  48.                begin
  49.                     SendMessage(Application.Handle,WM_SYSCOMMAND,SC_MONITORPOWER,0);                    
  50.                       end;
  51. //Activating screensaver
  52.               if (Action='5') then
  53.                begin
  54.                     DefWindowProc(Form1.Handle,WM_SYSCOMMAND,SC_SCREENSAVE,0);
  55.                      end;
  56.        end;
  57. end;
  58.  
  59. //Example
  60. //I use number 1-5 to represent the action because if I use long string it'll as case sensitive
  61. procedure TForm1.Button1Click(Sender: TObject);
  62. begin
  63. PowerMng('1',false); //Shutdown without force
  64. PowerMng('2',false); //Force to restart
  65. PowerMng('5',false); //Activing screensaver
  66. end;
  67.  
  68. //send your comment or greetz to determal@plasa.com
  • Author:
  • Date added:
    01 April, 2006
  • Views:
    2154
Latest News
Submit News Form Past News
Latest Forum Entries