Board index » delphi » Delay of shutdown

Delay of shutdown

Hi all!

I have alse been playing around with this for a long time, without success.
I added the below, and made my on test, which basically is:

------------------
procedure TForm1.Button1Click(Sender: TObject);
begin
  ExitWindowsEx(EWX_SHUTDOWN, 0);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  InitiateSystemShutdown(nil, pchar('this is it - the end!'), 5, False,
True) then
end;

function TForm1.GetShutDownPriv : boolean;
var
  htoken : THandle;
  tkp, p : TTokenPrivileges;
  retlen : dword;
  reply : dword;
begin
  Result := False;
  if OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES OR
TOKEN_QUERY, htoken) then
  begin
    if LookupPrivilegeValue(nil, 'SeShutdownPrivilege',
tkp.Privileges[0].luid) then
    begin
      tkp.PrivilegeCount := 1;
      tkp.Privileges[0].Attributes := Se_Privilege_Enabled;
      AdjustTokenPrivileges(htoken, False, tkp, SizeOf(tkp), p, retlen);
      reply := GetLastError;
      if reply = Error_Success then
        Result := True;
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  shutdown := True;
  GetShutDownPriv;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  ShutDown := False; // not called on shut down
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  if shutdown then
    AbortSystemShutdown(nil);
end;
--------------

And I found:

  AbortSystemShutdown will fail (returns false), if I try stop a shutdown
initiated by the Start button - Shut down - even when I get the privileges.
  But when using it after the InitiateSystemShutdown it work (I used a timer
to run 3 secs later) and it stops the shut down.
Apperently, these 2 functions are related, but not with the ExitWindows
function? Can anyone comment this?

I have been working on an app, where I simply need to delay the shutdown, as
mu programme needs 20 secs -> 5 mins when shutting down.

Now, is there any way to make this work, when the shut down is called from
the system itself? I have hoped, that this could do it, but it seems that it
cannot.

Rgds
Sonnich

 

Re:Delay of shutdown


Windows will not shut down until all program are finished running. Just try
to create a program with one form, and adjust the OnClose event so you can't
press the close button. Then try shutting down WIndows while the app. is
running. You can't.

Sonnich Jensen <sonnich.jen...@novobcs.ee> skrev i en
nyhedsmeddelelse:8a8e7n$hb...@news.estpak.ee...

Quote
> Hi all!

> I have alse been playing around with this for a long time, without
success.
> I added the below, and made my on test, which basically is:

> ------------------
> procedure TForm1.Button1Click(Sender: TObject);
> begin
>   ExitWindowsEx(EWX_SHUTDOWN, 0);
> end;

> procedure TForm1.Button2Click(Sender: TObject);
> begin
>   InitiateSystemShutdown(nil, pchar('this is it - the end!'), 5, False,
> True) then
> end;

> function TForm1.GetShutDownPriv : boolean;
> var
>   htoken : THandle;
>   tkp, p : TTokenPrivileges;
>   retlen : dword;
>   reply : dword;
> begin
>   Result := False;
>   if OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES OR
> TOKEN_QUERY, htoken) then
>   begin
>     if LookupPrivilegeValue(nil, 'SeShutdownPrivilege',
> tkp.Privileges[0].luid) then
>     begin
>       tkp.PrivilegeCount := 1;
>       tkp.Privileges[0].Attributes := Se_Privilege_Enabled;
>       AdjustTokenPrivileges(htoken, False, tkp, SizeOf(tkp), p, retlen);
>       reply := GetLastError;
>       if reply = Error_Success then
>         Result := True;
>     end;
>   end;
> end;

> procedure TForm1.FormCreate(Sender: TObject);
> begin
>   shutdown := True;
>   GetShutDownPriv;
> end;
> procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
> begin
>   ShutDown := False; // not called on shut down
> end;

> procedure TForm1.FormDestroy(Sender: TObject);
> begin
>   if shutdown then
>     AbortSystemShutdown(nil);
> end;
> --------------

> And I found:

>   AbortSystemShutdown will fail (returns false), if I try stop a shutdown
> initiated by the Start button - Shut down - even when I get the
privileges.
>   But when using it after the InitiateSystemShutdown it work (I used a
timer
> to run 3 secs later) and it stops the shut down.
> Apperently, these 2 functions are related, but not with the ExitWindows
> function? Can anyone comment this?

> I have been working on an app, where I simply need to delay the shutdown,
as
> mu programme needs 20 secs -> 5 mins when shutting down.

> Now, is there any way to make this work, when the shut down is called from
> the system itself? I have hoped, that this could do it, but it seems that
it
> cannot.

> Rgds
> Sonnich

Other Threads