Title: Shut down PC with windows OS
Question: How can I shut down windows with any provileges
Answer:
Is is also in article No.2108
But I think its very simple to understand.
At first place 3labels,timer,edit,button and name them like below.
unit MainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TFrmMain = class(TForm)
EdtTime: TEdit;
BtnAction: TButton;
MainTimer: TTimer;
LblTime: TLabel;
CountDown: TLabel;
LblShowCount: TLabel;
CDTimer: TTimer;
BtnStop: TButton;
procedure MainTimerTimer(Sender: TObject);
procedure BtnActionClick(Sender: TObject);
procedure CDTimerTimer(Sender: TObject);
procedure BtnStopClick(Sender: TObject);
Procedure ExitWindows(Flags: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FrmMain: TFrmMain;
Gharbilak : Integer;
implementation
uses Math;
Procedure ExitWindows(Flags: Integer);
var
Handle : THandle;
TokenPrivileges : TTokenPrivileges;
Size : Cardinal;
Begin
If not(OpenProcessToken(GetCurrentProcess,TOKEN_WRITE,Handle)) Then
RaiseLastOSError;
If not(LookupPrivilegeValue(nil, 'SeShutdownPrivilege', TokenPrivileges.Privileges[0].Luid)) Then
RaiseLastOSError;
TokenPrivileges.PrivilegeCount := 1;
TokenPrivileges.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
If not(AdjustTokenPrivileges(Handle,False,TokenPrivileges,0,nil,Size)) Then
RaiseLastOSError;
If not(ExitWindowsEx(Flags,0)) Then
RaiseLastOSError;
End;
{$R *.dfm}
procedure TFrmMain.MainTimerTimer(Sender: TObject);
begin
ExitWindows(EWX_POWEROFF or EWX_SHUTDOWN);
end;
procedure TFrmMain.BtnActionClick(Sender: TObject);
var
Time : Cardinal;
begin
Time := StrToInt(EdtTime.Text)*60000;
MainTimer.Interval := Time;
MainTimer.Enabled := True;
CDTimer.Enabled := True;
Gharbilak := StrToInt(FloatToStr(MainTimer.Interval / 1000 - 1));
end;
procedure TFrmMain.CDTimerTimer(Sender: TObject);
begin
LblShowCount.Caption := FloatToStr(Gharbilak);
Gharbilak := Gharbilak - 1;
if gharbilak end;
procedure TFrmMain.BtnStopClick(Sender: TObject);
begin
MainTimer.Enabled := False;
CDTimer.Enabled := False;
LblShowCount.Caption := 'Operation Canceled.';
end;
end.