you have to use ExitWindowsEx(EWX_FORCE+EWX_POWEROFF,0) to shut the computer
down. However you need to set the Privileges before you can shut the system.
Because Xp is based on NT. you would have to use
1.showmessOpenprocessToken
2.LookupPrivilegeValue('', SE_SHUTDOWN_NAME,tp.Privileges[0].Luid)
3.AdjustTokenPrivileges - this is function change privileges
the following is my simple code
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, ComCtrls, StdCtrls, Spin, Gauges;
type
TForm1 = class(TForm)
BStart: TButton;
BPause: TButton;
StatusBar1: TStatusBar;
Timer1: TTimer;
Panel1: TPanel;
Label1: TLabel;
Label2: TLabel;
houredit: TEdit;
minedit: TEdit;
secedit: TEdit;
Ltime: TLabel;
Label3: TLabel;
ProgressBar1: TProgressBar;
Timer2: TTimer;
procedure BStartClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure houreditChange(Sender: TObject);
procedure seceditChange(Sender: TObject);
procedure mineditChange(Sender: TObject);
procedure BPauseClick(Sender: TObject);
procedure Timer2Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
trigger, AM, PM, WM: boolean;
timetogo : integer;
end;
procedure shutdown;
var
Form1: TForm1;
implementation
{$R *.dfm}
const
SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
SE_REMOTE_SHUTDOWN_NAME = 'SeRemoteShutdownPrivilege';
procedure shutdown;
var
hToken: THANDLE; //process token
tp, oldtp:TTokenPrivileges;// Token provileges
dwsize :Cardinal;
// luid: TLargeInteger;
begin
dwsize:=sizeof(TTokenPrivileges);
if ( not rrentprocess(), TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, hTOKEN)) then
showmessOpenprocessToken(getcuage('OpenProcessToken failed');
if ( not LookupPrivilegeValue('', SE_SHUTDOWN_NAME,tp.Privileges[0].Luid)) then
showmessage('LookupPrivilegeValue failed');
tp.PrivilegeCount:=1; //Specifies the number of entries in the Privileges array.
// tp.Privileges[0].Luid:=luid;
tp.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken,FALSE,tp,sizeof(TTokenPrivileges),Oldtp,dwSize);
if GetLastError <> ERROR_SUCCESS then
raise Exception.Create('AdjustTokenPrivileges enable failed.');
end;
procedure TForm1.BStartClick(Sender: TObject);
var
DateTime : TDateTime;
str,hour,min,sec : string;
begin
// timer1.Enabled:=true;
DateTime := Time; // store the current date and time
str := TimeToStr(DateTime); // convert the time into a string
hour:= copy(str,0,2);
min:= copy(str,4,2);
sec:= copy(str,7,2);
timetogo:= 3600*(strtoint(houredit.Text))+60*(strtoint(minedit.text))+ strToint(secedit.Text)
-3600*(strtoint(hour)) - 60*(strtoint(min)) - strToint(sec);
timer2.Enabled:=true;
Progressbar1.Max:=timetogo;
trigger:=true;
houredit.Enabled:=false;
secedit.Enabled:=false;
minedit.Enabled:=false;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
DateTime : TDateTime;
str,s : string;
begin
DateTime := Time; // store the current date and time
str := TimeToStr(DateTime); // convert the time into a string
Ltime.Caption:= str; // display the time on the label's caption
s:= copy(str,9,4);
if s='AM' then AM:=true; //check the time is in am or pm
if s='PM' then PM:=true;
if s='' then WM:=true; //check the system time is 24 hours or 12 hours format
if trigger then
begin
if WM then
if str = houredit.Text + ':' + minedit.Text + ':' + secedit.Text then
begin
shutdown;
sleep(1000);
if (not ExitWindowsEx(EWX_FORCE+EWX_POWEROFF,0)) then
showmessage('Exitwindows failed');
// initiateSystemShutdown(nil,nil,0,true,false);
end
else
begin
if str = houredit.Text + ':' + minedit.Text + ':' + secedit.Text +' ' + s then
begin
shutdown;
// initiateSystemShutdown(nil,nil,0,true,false);
sleep(1000);
if (not ExitWindowsEx(EWX_FORCE+EWX_POWEROFF,0)) then
showmessage('Exitwindows failed');
end;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
// ctedit.Text:= TimeToStr(time);
trigger:= False;
end;
procedure TForm1.houreditChange(Sender: TObject);
var
max:integer;
begin
if wm then max:= 24 else max:=12;
if houredit.Text='' then exit;
if strToint(houredit.Text) > max then
houredit.Text:=inttostr(max);
if strToint(houredit.Text) < 0 then
houredit.Text:='0';
end;
procedure TForm1.seceditChange(Sender: TObject);
begin
if secedit.Text='' then exit;
if strToint(secedit.Text) > 60 then
secedit.Text:='60';
if strToint(secedit.Text) < 0 then
secedit.Text:='0';
end;
procedure TForm1.mineditChange(Sender: TObject);
begin
if minedit.Text='' then exit;
if strToint(minedit.Text) > 60 then
minedit.Text:='60';
if strToint(minedit.Text) < 0 then
minedit.Text:='0';
end;
procedure TForm1.BPauseClick(Sender: TObject);
begin
houredit.Enabled:=true;
minedit.Enabled:=true;
timer2.Enabled:=false;
trigger:=false;
end;
procedure TForm1.Timer2Timer(Sender: TObject);
begin
Progressbar1.StepBy(1);//(timetogo-stoptime);
end;
end.