Board index » delphi » madshi terminateprocess hook + windows shutdown

madshi terminateprocess hook + windows shutdown


2005-06-03 05:09:07 PM
delphi43
I have used madshi code to hook the terminateprocess and avoid my
application from being killed from the task manager. Everything is ok but I
have a problem on that.
When user tries to reboot or logoff, my application (due to the unkillable
power) it denies the action. So, I added some piece of code to avoid this:
procedure WMQueryEndSession(var Msg: TWMQueryEndSession); message
WM_QUERYENDSESSION;
procedure WMEndSession(var Msg: TWMEndSession); message
WM_ENDSESSION;
Im detecting the reboot/logoff and I terminate myself so everything is fine.
Some users found a trick. They open Wordpad (or word) and they type some
words. Then they ask to logoff the machine.
1. My application terminates itself
2. Wordpad asks if he wants to save the file. He clicks "Cancel" and windows
shutdown is canceled.
the result? my application is killed and everything is fine on the machine.
How can I solve this problem? I want to be the last one that will be
informed for the windows shutdown. Any idea?
Thanks
 
 

Re:madshi terminateprocess hook + windows shutdown

IIRC, one of the parameters tells you if the shutdown happened or not. If
cancelled, the apps are told about it.
/Matthew Jones/
 

Re:madshi terminateprocess hook + windows shutdown

Simply avoid to terminate already on WM_QUERYENDSESSION.
Windows sends this message around to ask the applications if they
object to shutdown.
WM_ENDSESSION is only sent if all applications had no objections.
It tells the applications that they should not expect a WM_QUIT anymore
because the applications get not terminated cleanly on shutdown.
 

Re:madshi terminateprocess hook + windows shutdown

Nick Rollas writes:
Quote
How can I solve this problem? I want to be the last one that will be
informed for the windows shutdown. Any idea?
Wouldn't a service application be more appropriate to what you want to
achieve? In this way your app could run independent of the user and
would even run after the user has logged off.
If you need to have a gui you can do that via a separate application.
Jan
 

Re:madshi terminateprocess hook + windows shutdown

Nick Rollas writes:
Quote
I tried removing the WM_QUERYENDSESSION and leaving only the WM_ENDSESSION
but it doesnt work.
You will of course still have to handle WM_QUERYENDSESSION. Set
"Msg.Result := 1;" to signal acceptance of shutdown.
 

Re:madshi terminateprocess hook + windows shutdown

No its, a huge project with 40 forms, database, mysql, etc. A seperate
service is a little trouble for me. I think a better solution would appear.
Thanks anyway :)
"Jan Mitrovics" <Mitrovics-at-web.de>writes
Quote
Nick Rollas writes:

>How can I solve this problem? I want to be the last one that will be
>informed for the windows shutdown. Any idea?


Wouldn't a service application be more appropriate to what you want to
achieve? In this way your app could run independent of the user and
would even run after the user has logged off.

If you need to have a gui you can do that via a separate application.

Jan
 

Re:madshi terminateprocess hook + windows shutdown

I tried removing the WM_QUERYENDSESSION and leaving only the WM_ENDSESSION
but it doesnt work.
Maybe there is a trick to tell that my application is the last one that will
be asked. Something like chaging the internal order.
Nick
"Robert Marquardt" <XXXX@XXXXX.COM>writes
Quote
Simply avoid to terminate already on WM_QUERYENDSESSION.
Windows sends this message around to ask the applications if they
object to shutdown.
WM_ENDSESSION is only sent if all applications had no objections.
It tells the applications that they should not expect a WM_QUIT anymore
because the applications get not terminated cleanly on shutdown.

 

Re:madshi terminateprocess hook + windows shutdown

That is that I had when I posted here.
I had the WM_QUERYENDSESSION with Msg.Result :=1
then I also tried removing it.
Both cases the same problem.
Nick
"Robert Marquardt" <XXXX@XXXXX.COM>writes
Quote
Nick Rollas writes:

>I tried removing the WM_QUERYENDSESSION and leaving only the
>WM_ENDSESSION but it doesnt work.

You will of course still have to handle WM_QUERYENDSESSION. Set
"Msg.Result := 1;" to signal acceptance of shutdown.
 

Re:madshi terminateprocess hook + windows shutdown

Nick Rollas writes:
Quote
No its, a huge project with 40 forms, database, mysql, etc. A
seperate service is a little trouble for me. I think a better
solution would appear.

Thanks anyway :)
You could do as the viruses do. Write a service that checks if you app
is not running and if not then it starts it.
Cheers
Dean
 

Re:madshi terminateprocess hook + windows shutdown

As far as I remember, win9x worked like this:
- send WM_QUERYENDSESSION to all processes
- if anybody denied shutdown, abort shutdown
- WM_ENDSESSION + close all processes
- shutdown
And winNT works like this:
- for each process do
- send WM_QUERYENDSESSION to process x
- if it denies shutdown, abort shutdown
- WM_ENDSESSION + close process x
- end
- shutdown
--
www.madshi.net
high quality low level Delphi components
extended exception handling
API hooking, DLL injection
 

Re:madshi terminateprocess hook + windows shutdown

Well, that is really a difficult problem. The NT
family just doesn't give you control over in which
order it shall ask processes about shutdown.
What you could do is this: Deny WM_QUERYENDSESSION.
When the user tries to shutdown, open a box saying:
"If you want to shutdown, then press the shutdown
button in my app".
If the shutdown button in your app is pressed, you
could tell the user: "please close all running apps
now". When the user confirms that he closed all
apps, start a shutdown on your own with EWX_FORCE.
That's definately no nice solution, though.
--
www.madshi.net
high quality low level Delphi components
extended exception handling
API hooking, DLL injection
 

Re:madshi terminateprocess hook + windows shutdown

The problem is that people cant shutdown the program without a password
(only admin knows it). So, they will use it as a backdoor to prevent the
security of the application.
I think its oneway :(
Nick
"madshi (Mathias Rauen)" <XXXX@XXXXX.COM>writes
Quote
Well, that is really a difficult problem. The NT
family just doesn't give you control over in which
order it shall ask processes about shutdown.

What you could do is this: Deny WM_QUERYENDSESSION.
When the user tries to shutdown, open a box saying:

"If you want to shutdown, then press the shutdown
button in my app".

If the shutdown button in your app is pressed, you
could tell the user: "please close all running apps
now". When the user confirms that he closed all
apps, start a shutdown on your own with EWX_FORCE.

That's definately no nice solution, though.

--
www.madshi.net
high quality low level Delphi components
extended exception handling
API hooking, DLL injection
 

Re:madshi terminateprocess hook + windows shutdown

Consider using a service and the SvCom code from www.aldyn.ru which allows
you to have a full app with GUI and all run as a service. It works well
for me.
/Matthew Jones/
 

Re:madshi terminateprocess hook + windows shutdown

Nick Rollas writes:
Quote
How can I solve this problem? I want to be the last one that will be
informed for the windows shutdown. Any idea?
You cannot (reliably) achieve that.
Let's assume for a moment that such a feature, hack, trick, API or
whatever exists. You utililize it in your application, and all works
well - your application is the last one getting the notification.
Anything done by your application can be done by another one as well, so
what would happen if another application would do the same thing?
You can of course try to think various ways to overcome the issue of two
applications, but in essence the problem stays regardless of the
workarounds. I am sure that one can think of a workable special case, but
any such case would not be practically feasible.