Article View: borland.public.cppbuilder.nativeapi
Article #2384Re: My app closes, but cancels Windows shutdown?
From: A Nonymous
Date: Thu, 08 Mar 2007 22:54
Date: Thu, 08 Mar 2007 22:54
147 lines
6247 bytes
6247 bytes
"Dennis Jones" <nospam@nospam.com> wrote in
news:45f08f8f$1@newsgroups.borland.com:
>
> "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
>
>
>
I need to do the terminate in the WM_QUERYENDSESSION because the files
are on the net and I need to bypass the normal cleanup code. When I
used to do it in the WM_ENDSESSION sometimes it got runtime errors
because other services had been shut down. When I moved everything to
the WM_QUERYENDSESSION and started using the terminate the shutdown was
100% reliable. It introduces a small memory leak, but when you are
rebooting this is not a problem.
This has worked for a couple of years, but about 6 mos ago I did a major
overhaul to the user interface and added a lot of graphics, a skin
capability and foreign language support. In the process I added a lot
of 3rd party components, and updated several of the component suites I
was using. Obviously I of these changes broke something I am just not
sure how to narrow it down.
Technical Details
Message-ID:
<Xns98EDAC20DAA4Eanonymoushotmailcom@207.105.83.66>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-mailReferences:
<Xns98ED90968E734anonymoushotmailcom@207.105.83.66> <45f07307@newsgroups.borland.com> <Xns98ED988E421A8anonymoushotmailcom@207.105.83.66> <45f08f8f$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,346f60f70c29a156 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: My app closes, but cancels Windows shutdown? From: A Nonymous <anonymous@hotmail.com> References: <Xns98ED90968E734anonymoushotmailcom@207.105.83.66> <45f07307@newsgroups.borland.com> <Xns98ED988E421A8anonymoushotmailcom@207.105.83.66> <45f08f8f$1@newsgroups.borland.com> Message-ID: <Xns98EDAC20DAA4Eanonymoushotmailcom@207.105.83.66> User-Agent: Xnews/5.04.25 NNTP-Posting-Host: 74.254.162.162 Date: 8 Mar 2007 14:54:43 -0800 X-Trace: newsgroups.borland.com 1173394483 74.254.162.162 (8 Mar 2007 14:54:43 -0800) Lines: 147 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