🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #2378

Re: My app closes, but cancels Windows shutdown?

#2378
From: Dennis Jones
Date: Thu, 08 Mar 2007 22:35
116 lines
5168 bytes
"A Nonymous" <anonymous@hotmail.com> wrote in message 
news:Xns98ED988E421A8anonymoushotmailcom@207.105.83.66...

>> According to the documentation, the return value for WM_ENDSESSION is
>> 0, not 1.
>>
> It doesn't appear to work that way.  You may not have seen my addendum
> when you posted this response, but take a look at it.

I think you are right (I believe Remy is mistaken here).  One of my 
applications processes the WM_QUERYENDSESSION and WM_ENDSESSION messages, 
and I do it this way (and it works perfectly):

MESSAGE void __fastcall TMainForm::WMEndSession(TMessage &msg)
{
   // if WM_QUERYENDSESSION returned false, it's not okay to shutdown the 
program
   if ( msg.WParam == false ) return;

   // if we get this far, it's okay to shut down the app
   LogMsg("System is shutting down - Received end of session notification");

   // if Windows is shutting down, we want to shut down the application 
without user intervention;
   // to accomplish this, we must stop the service first
   SendMessage( this->Handle, WM_SERVICE_STOP, NoPrompt, 0 );

   // now we know the service is not running; we can close the application 
and we
   // won't be queried about shutting down
   Close();
}
//---------------------------------------------------------------------------

// handle WM_QUERYENDSESSION -- this isn't strictly necessary since we 
always want to allow
// Windows to shut down, but it's here for completeness and for the sake of 
logging
MESSAGE void __fastcall TMainForm::WMQueryEndSession(TMessage &msg)
{
   LogMsg("System wants to shut down - Received end of session query 
notification");

   // when an application receives the WM_QUERYENDSESSION message, it must 
tell Windows
   // whether or not it is really okay to shut down;
   // setting the message result to 'true' means it is okay to shut down 
Windows
   msg.Result = true;
}
//---------------------------------------------------------------------------


> To simplify the problem I created a new application.  I created only a
> handlet for the WM_QUERYENDSESSION and WM_ENDSESSION messages.
>
> void __fastcall TFormMain::wmQueryEndSession(TMessage& Message)
> {
>  Message.Result = 1;
>  Application->Terminate();
> }

That seems fishy to me (calling Terminate from the WM_QUERYENDSESSION 
handler).  Perhaps you should wait until WM_ENDSESSION is received before 
performing any shutdown procedures?


> void __fastcall TFormMain::wmEndSession(TMessage& Message)
> {
>  Message.Result = 1;
> }

Not sure if the Result field needs to be set in WM_ENDSESSION (I don't do it 
in my app).


> The app terminates and Windows restarts.

Comparing your code to mine, I believe that is correct and expected.


> I changed the Message.Result to 0 (in both) and the application
> terminated but Windows did not restart.

Right, setting Message.Result = 0 in WM_QUERYENDSESSION should abort the OS 
shutdown.


> This is the problem I am trying
> to resolve.
>
> I used my "fake" shutdown app to send the shutdown messages to both
> versions of the test applcation and with Message.Result = 1, the app
> shuts down, I get a true response from the SnedMessage and both wParam
> and lParam are 0.
>
> The same operation with Message.Result = 0, the app shuts down, I get a
> FALSE response from the SnedMessage and both wParam and lParam are still
> 0.
>
> On my real app, the app shuts down, I get a true response from the
> SnedMessage and both wParam and lParam are 0, BUT Windows does not
> restart.
>
> There must be something else?  I just don't know how to find it.

The only thing that seems odd to me is your WM_QUERYENDSESSION handler. 
Terminating the application from there seems premature to me.  It ought to 
work okay, but I just can't help wondering if that has anything to do with 
it (I know, I'm grasping at straws here).  Instead of calling 
Application->Terminate from your WM_WNEDQUERYSESSION handler, can you try 
calling Close() from the WM_ENDSESSION handler (as I do in the snippets I 
posted above)?

- Dennis 
Technical Details
Message-ID: <45f08f8f$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <Xns98ED90968E734anonymoushotmailcom@207.105.83.66> <45f07307@newsgroups.borland.com> <Xns98ED988E421A8anonymoushotmailcom@207.105.83.66>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,346f60f70c29a156
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Dennis Jones" <nospam@nospam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <Xns98ED90968E734anonymoushotmailcom@207.105.83.66> <45f07307@newsgroups.borland.com> <Xns98ED988E421A8anonymoushotmailcom@207.105.83.66>
Subject: Re: My app closes, but cancels Windows shutdown?
Date: Thu, 8 Mar 2007 14:35:27 -0800
Lines: 118
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3028
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028
X-RFC2646: Format=Flowed; Original
NNTP-Posting-Host: 71.245.109.58
Message-ID: <45f08f8f$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1173393295 71.245.109.58 (8 Mar 2007 14:34:55 -0800)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail