Board index » delphi » Trouble with windows shutdown/power off using D5

Trouble with windows shutdown/power off using D5

A friend asked me to write him something that turns his win98 machine
off after a set time...

Easy I thought, and sent him a test form with a button containing....
ExitWindowsEx(EWX_POWEROFF,0);

He said it behaved like a logoff, and asked for a user name...

I tried it on Windows 2000 (logged in as admin) and it does nothing at
all. I sat and clicked it several times, and it had no effect on
anything!

I then tried...
ExitWindowsEx(ewx_logoff+EWX_poweroff,0);

But that didn't seem to do it either...

Anyone got any ideas?

Dodgy.

 

Re:Trouble with windows shutdown/power off using D5


Before making the call to ExitWindowsEx you should first call
Application.Terminate.

That should work.

HTH,

Roy

Dodgy <Do...@uk.earth> schreef in berichtnieuws
95c7usga5imu71ajvsq30fnvgvim7me...@4ax.com...

Quote
> A friend asked me to write him something that turns his win98 machine
> off after a set time...

> Easy I thought, and sent him a test form with a button containing....
> ExitWindowsEx(EWX_POWEROFF,0);

> He said it behaved like a logoff, and asked for a user name...

> I tried it on Windows 2000 (logged in as admin) and it does nothing at
> all. I sat and clicked it several times, and it had no effect on
> anything!

> I then tried...
> ExitWindowsEx(ewx_logoff+EWX_poweroff,0);

> But that didn't seem to do it either...

> Anyone got any ideas?

> Dodgy.

Re:Trouble with windows shutdown/power off using D5


The logic of that completely escapes me!
Surely with the app.terminate before the exitwindowsex call it will
never execute it!

Okay, I have now managed to get my friend to test with win98 and
  ExitWindowsEx(ewx_logoff+EWX_poweroff,0);
  application.terminate;

works great...

Unfortunately windows 2000 just looks at me blankly! (well it ignores
it completely).
(no matter where the app.terminate is!)

Dodgy.

On Wed, 11 Oct 2000 14:01:08 +0200, "Roy Rutten"
<remove.no.junkmail.roy.rut...@xarmac.nl> waffled on about something:

Quote
>Before making the call to ExitWindowsEx you should first call
>Application.Terminate.

>That should work.

>HTH,

>Roy

>Dodgy <Do...@uk.earth> schreef in berichtnieuws
>95c7usga5imu71ajvsq30fnvgvim7me...@4ax.com...
>> A friend asked me to write him something that turns his win98 machine
>> off after a set time...

>> Easy I thought, and sent him a test form with a button containing....
>> ExitWindowsEx(EWX_POWEROFF,0);

>> He said it behaved like a logoff, and asked for a user name...

>> I tried it on Windows 2000 (logged in as admin) and it does nothing at
>> all. I sat and clicked it several times, and it had no effect on
>> anything!

>> I then tried...
>> ExitWindowsEx(ewx_logoff+EWX_poweroff,0);

>> But that didn't seem to do it either...

>> Anyone got any ideas?

>> Dodgy.

Re:Trouble with windows shutdown/power off using D5


"Dodgy" <Do...@uk.earth> skrev i melding
news:bnv8usk12fm02hk9rd133puqe8juarq15r@4ax.com...

Quote
> The logic of that completely escapes me!
> Surely with the app.terminate before the exitwindowsex call it will
> never execute it!

from FORMS.PAS:

procedure TApplication.Terminate;
begin
  if CallTerminateProcs then PostQuitMessage(0);
end;

..the PostQuitMessage call puts a WM_QUIT message in the message queue
and returns immediately. Only  when all normal processing is finished,
this message will drop in...so it's no magic...;-)
The same technique may be used when you want some processing to start
after all initialzation is finished on startup, you just post yourself a
custom message, and uses the according message handler method to start
the processing.

The point in program execution such messages will be processed, is the
same point where the TApplication.OnIdle event would be reached (if
message wasn't posted) - when no pending messages are queued.

Quote
> Okay, I have now managed to get my friend to test with win98 and
>   ExitWindowsEx(ewx_logoff+EWX_poweroff,0);
>   application.terminate;

> works great...

> Unfortunately windows 2000 just looks at me blankly! (well it ignores
> it completely).
> (no matter where the app.terminate is!)

Never sat my foot in a win2000 so far...sounds like you don't have
permission to shut down the machine - have you tried with only
ewx_logoff ?

--
Bjoerge Saether
Consultant / Developer
Asker, Norway
bsaether.removet...@online.no (remove the obvious)

Re:Trouble with windows shutdown/power off using D5


Quote
> The logic of that completely escapes me!
> Surely with the app.terminate before the exitwindowsex call it will
> never execute it!

Yes it will. The call to Application.Terminate does not execute the
termination right away. The function posts a message which will be executed
when the main thread in your programm is idle (so sometime after your
ButtonClick handler has been executed). The same goes for ExitWindowsEx. So
the correct order is to first terminate the application and then shutdown
windows.

Quote
> Okay, I have now managed to get my friend to test with win98 and
>   ExitWindowsEx(ewx_logoff+EWX_poweroff,0);
>   application.terminate;

> works great...

> Unfortunately windows 2000 just looks at me blankly! (well it ignores
> it completely).
> (no matter where the app.terminate is!)

Don't know about windows 2000, haven't used delphi yet on that OS.

Re:Trouble with windows shutdown/power off using D5


Im Artikel <95c7usga5imu71ajvsq30fnvgvim7me...@4ax.com>, Dodgy <Do...@uk.earth>
schreibt:

Quote
>ExitWindowsEx(EWX_POWEROFF,0);

From online help:
---
EWX_POWEROFF    Shuts down the system and turns off the power.
The system must support the power-off feature.
Windows NT/2000: The calling process must have the SE_SHUTDOWN_NAME privilege.
---

Perhaps you have that privilege, as admin, but the process doesn't have it
automatically?

I also found:
---
EWX_SHUTDOWN Shuts down the system to a point at which it is safe to turn off
the power. All file buffers have been flushed to disk, and all running
processes have stopped.
---
and
---
EWX_POWEROFF Windows 2000: Forces processes to terminate if they do not respond
to the WM_QUERYENDSESSION or WM_ENDSESSION message. This flag is ignored if
EWX_FORCE is used.
---

Since a poweroff always should be preceded by a shutdown, perhaps some
application didn't respond to, or deny, shutdown?

DoDi

Re:Trouble with windows shutdown/power off using D5


Quote
On Wed, 11 Oct 2000 01:17:18 +0100, Dodgy <Do...@uk.earth> wrote:
>A friend asked me to write him something that turns his win98 machine
>off after a set time...

>Easy I thought, and sent him a test form with a button containing....
>ExitWindowsEx(EWX_POWEROFF,0);

>He said it behaved like a logoff, and asked for a user name...

>I tried it on Windows 2000 (logged in as admin) and it does nothing at
>all. I sat and clicked it several times, and it had no effect on
>anything!

>I then tried...
>ExitWindowsEx(ewx_logoff+EWX_poweroff,0);

>But that didn't seem to do it either...

>Anyone got any ideas?

var
  TokenHandle: THandle;
  Privileges: TTokenPrivileges;
begin
  if Win32Platform = VER_PLATFORM_WIN32_NT then begin
    Privileges.PrivilegeCount := 1;
    if not LookupPrivilegeValue('', 'SeShutdownPrivilege',
        Privileges.Privileges[0].Luid) then
      RaiseLastWin32Error;
    Privileges.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
    if not OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES,
        TokenHandle) then
      RaiseLastWin32Error;
    try
      if not AdjustTokenPrivileges(TokenHandle, false, Privileges, 0,
          PTokenPrivileges(nil)^, PDWORD(nil)^) then
        RaiseLastWin32Error;
    finally
      CloseHandle(TokenHandle);
    end;
  end;
  if not ExitWindowsEx(EWX_POWEROFF, 0) then
    RaiseLastWin32Error;
end;

--
The children were playing 'Other Kind of People', and I asked them what
it meant.  "We play we're the Other Kind of People, and nobody can tell
we're not regular people," they said.  "They can't even guess it."

Re:Trouble with windows shutdown/power off using D5


D'oh!

Me feel stupid now!

Dodgy.

On Thu, 12 Oct 2000 08:30:20 +0200, "Roy Rutten"
<remove.no.junkmail.roy.rut...@xarmac.nl> waffled on about something:

Quote
>> The logic of that completely escapes me!
>> Surely with the app.terminate before the exitwindowsex call it will
>> never execute it!

>Yes it will. The call to Application.Terminate does not execute the
>termination right away. The function posts a message which will be executed
>when the main thread in your programm is idle (so sometime after your
>ButtonClick handler has been executed). The same goes for ExitWindowsEx. So
>the correct order is to first terminate the application and then shutdown
>windows.

>> Okay, I have now managed to get my friend to test with win98 and
>>   ExitWindowsEx(ewx_logoff+EWX_poweroff,0);
>>   application.terminate;

>> works great...

>> Unfortunately windows 2000 just looks at me blankly! (well it ignores
>> it completely).
>> (no matter where the app.terminate is!)

>Don't know about windows 2000, haven't used delphi yet on that OS.

Re:Trouble with windows shutdown/power off using D5


I have had a play, and logoff works, so it's got to be this
SE_SHUTDOWN_NAME bit....

A couple of questions....
1) Why does a user with admin access not have it? It can do everything
else!
2) How can I get it?

Thanks in advance,

Dodgy.

On 12 Oct 2000 21:43:44 GMT, vb...@aol.com (VBDis) waffled on about
something:

Quote
>Im Artikel <95c7usga5imu71ajvsq30fnvgvim7me...@4ax.com>, Dodgy <Do...@uk.earth>
>schreibt:

>>ExitWindowsEx(EWX_POWEROFF,0);

>From online help:
>---
>EWX_POWEROFF        Shuts down the system and turns off the power.
>The system must support the power-off feature.
>Windows NT/2000: The calling process must have the SE_SHUTDOWN_NAME privilege.
>---

>Perhaps you have that privilege, as admin, but the process doesn't have it
>automatically?

>I also found:
>---
>EWX_SHUTDOWN Shuts down the system to a point at which it is safe to turn off
>the power. All file buffers have been flushed to disk, and all running
>processes have stopped.
>---
>and
>---
>EWX_POWEROFF Windows 2000: Forces processes to terminate if they do not respond
>to the WM_QUERYENDSESSION or WM_ENDSESSION message. This flag is ignored if
>EWX_FORCE is used.
>---

>Since a poweroff always should be preceded by a shutdown, perhaps some
>application didn't respond to, or deny, shutdown?

>DoDi

Re:Trouble with windows shutdown/power off using D5


Ohhh... What a lovely man! :o)

That works a treat!

Thanks David.

On Fri, 13 Oct 2000 03:04:45 GMT, drifk...@acm.deleteme.org (David
Rifkind) waffled on about something:

Quote
>On Wed, 11 Oct 2000 01:17:18 +0100, Dodgy <Do...@uk.earth> wrote:
>>A friend asked me to write him something that turns his win98 machine
>>off after a set time...

>>Easy I thought, and sent him a test form with a button containing....
>>ExitWindowsEx(EWX_POWEROFF,0);

>>He said it behaved like a logoff, and asked for a user name...

>>I tried it on Windows 2000 (logged in as admin) and it does nothing at
>>all. I sat and clicked it several times, and it had no effect on
>>anything!

>>I then tried...
>>ExitWindowsEx(ewx_logoff+EWX_poweroff,0);

>>But that didn't seem to do it either...

>>Anyone got any ideas?

>var
>  TokenHandle: THandle;
>  Privileges: TTokenPrivileges;
>begin
>  if Win32Platform = VER_PLATFORM_WIN32_NT then begin
>    Privileges.PrivilegeCount := 1;
>    if not LookupPrivilegeValue('', 'SeShutdownPrivilege',
>        Privileges.Privileges[0].Luid) then
>      RaiseLastWin32Error;
>    Privileges.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
>    if not OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES,
>        TokenHandle) then
>      RaiseLastWin32Error;
>    try
>      if not AdjustTokenPrivileges(TokenHandle, false, Privileges, 0,
>          PTokenPrivileges(nil)^, PDWORD(nil)^) then
>        RaiseLastWin32Error;
>    finally
>      CloseHandle(TokenHandle);
>    end;
>  end;
>  if not ExitWindowsEx(EWX_POWEROFF, 0) then
>    RaiseLastWin32Error;
>end;

Other Threads