How intercept shutdown or logoff ?

Just starting out? Need help? Post your questions and find answers here.
PicardDontoro
New User
New User
Posts: 5
Joined: Fri Aug 06, 2004 2:55 pm

How intercept shutdown or logoff ?

Post by PicardDontoro »

Hi,

how I can intercept from my application when Windows shutdown or logoff ?

And if it's possible, Windows can wait that my application makes various operations before it shutdown/logoff ?

TIA
User avatar
Derlidio
User
User
Posts: 77
Joined: Fri Feb 27, 2004 9:19 pm
Location: SP - Brazil

Post by Derlidio »

In order to do that, you must inspect the messages Windows send to your app. It may be done by using SetWindowCallback() function:

Code: Select all

Declare.l MyWindowCallback(WindowID, Message, wParam, lParam)

OpenWindow(0,0,0,320,200,#PB_Window_SystemMenu, "Test Window")

SetWindowCallback(@MyWindowCallback())

Procedure.l MyWindowCallback(WindowID, Message, wParam, lParam) 

  Result = #PB_ProcessPureBasicEvents 
  
  If Message = $16 ; End Session Message
      If wParam 
         ; the section is ending
         If lParam
            ;user is logging off
            MessageRequester("X", "User is is logging off.")
            Else
            ;user is shutting down the system.
            MessageRequester("X", "User is shutting down the system.")
         EndIf
      EndIf
  EndIf
  
  ProcedureReturn Result 
  
EndProcedure 

Repeat
  If WaitWindowEvent() = #PB_Event_CloseWindow: Break: EndIf
ForEver
But I can't tell how many time the system will wait until forcing your app to close. May be (I'm not sure :oops: ) that Windows will query your app from time to time sending a message $11 to see if it is all right to shutdown the system. In that case, I think you should return 0 (zero) to the caller (instead of the default Result) until your app is done with the cean-up. As I said, I'm not sure about that.

Well, at least it is a good start point, I think :wink:
Derlidio Siqueira
freak
PureBasic Team
PureBasic Team
Posts: 5944
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

There are two messages: #WM_QUERYENDSESSION and #WM_ENDSESSION
You receive #WM_QUERYENDSESSION first, there you can choose wether
you want windows to actually shut down or not, by returning 0, you tell
Windows, that your program is not ready to be shut down, and by returning 1,
you indicate that your program can be shut down now.

After that, you receive the #WM_ENDSESSION, which tells you that your
program will now be terminated. After you return from the callback procedure
when you get this message, windows will terminate your app.

See here:
http://msdn.microsoft.com/library/defau ... ession.asp

Timo
quidquid Latine dictum sit altum videtur
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: How intercept shutdown or logoff ?

Post by Vera »

Hi,

I stumbled over this thread and found the MSDN link above is dead
so I searched for the actual one : http://msdn.microsoft.com/en-us/library/aa376890(VS.85).aspx

~ Vera
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re:

Post by PB »

> You receive #WM_QUERYENDSESSION first, there you can choose wether
> you want windows to actually shut down or not, by returning 0, you tell
> Windows, that your program is not ready to be shut down, and by returning 1

Yep, but remember this is not 100% reliable, as Windows will disobey it if it wants. Happened to me the other day. So if your app is expecting to halt Windows in its tracks, you'll be kidding yourself. (Not you personally, Freak). Just something to be aware of. In other words, if you detect that Windows is shutting down, you'd best just do all your shutdown stuff FAST instead of trying to stop Windows...
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Post Reply