//I guess it will works in all windows
function PowerMng(Action:String;Force:Boolean):boolean;
var rl: Cardinal;
hToken: Cardinal;
tkp: TOKEN_PRIVILEGES;
begin
if Win32Platform = VER_PLATFORM_WIN32_NT then
begin
//Get access to windows privilege
OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken);
LookupPrivilegeValue(nil, 'SeShutdownPrivilege', tkp.Privileges[0].Luid);
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;;
tkp.PrivilegeCount := 1;
AdjustTokenPrivileges(hToken, False, tkp, 0, nil, rl);
//Shutdown Windows
if (Action='1') and (Force=false) then
begin
ExitWindowsEx(EWX_SHUTDOWN, 0)
end else
if (Action='1') and (Force=true) then
begin
(ExitWindowsEx(EWX_SHUTDOWN or EWX_FORCE, 0));
end;
//Restart/Reboot Windows
if (Action='2') and (Force=false) then
begin
ExitWindowsEx(EWX_REBOOT, 0)
end else
if (Action='2') and (Force=true) then
begin
(ExitWindowsEx(EWX_REBOOT or EWX_FORCE, 0));
end;
//Log Off Windows
if (Action='3') and (Force=false) then
begin
ExitWindowsEx(EWX_LOGOFF, 0)
end else
if (Action='3') and (Force=true) then
begin
(ExitWindowsEx(EWX_LOGOFF or EWX_FORCE, 0));
end;
//Turn off monitor
if (Action='4') and (Force=true) then
begin
SendMessage(Application.Handle,WM_SYSCOMMAND,SC_MONITORPOWER,2);
end else
if (Action='4') and (Force=true) then //Turn on monitor
begin
SendMessage(Application.Handle,WM_SYSCOMMAND,SC_MONITORPOWER,0);
end;
//Activating screensaver
if (Action='5') then
begin
DefWindowProc(Form1.Handle,WM_SYSCOMMAND,SC_SCREENSAVE,0);
end;
end;
end;
//Example
//I use number 1-5 to represent the action because if I use long string it'll as case sensitive
procedure TForm1.Button1Click(Sender: TObject);
begin
PowerMng('1',false); //Shutdown without force
PowerMng('2',false); //Force to restart
PowerMng('5',false); //Activing screensaver
end;
//send your comment or greetz to determal@plasa.com