Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WM_ENDSESSION and WM_QUERYENDSESSION problem

Status
Not open for further replies.

petyo

Programmer
Feb 28, 2001
5
0
0
BG
Hello!
I use Windows 98 and BCB.
I develop a password protected application which resides ion systray. I wish when windows shuts down to stop my application without showing password dialog. Here is my code:
...
void __fastcall TMainForm::WndProc( TMessage &Msg)
{
switch ( Msg.Msg )
{
case WM_ENDSESSION:
if ( Msg.WParam )
{
bForceClose = true;
Close();
Msg.Result = 0;
}
break;

case WM_QUERYENDSESSION:
bForceClose = true;
Msg.Result = true;
break;

default: break;
}
inherited::WndProc( Msg);
}
...
void __fastcall TMainForm::FormCloseQuery(TObject *Sender, bool &CanClose)
{
if ( bForceClose )
{
CanClose = true;
return;
}
...//showing password dialog, etc.
}
....

The result is: my program stops (without password dialog) but windows refuses to shudown. It shuts down at the second attempt when the program is not running. On the other hand if there is an unsaved document (for example in notepad), when windows shuts down the user will be asked to save the document and if he/she clicks
"Cancel" button the shut down process stops. I need my program to run all the
time windows is running.
Where am I wrong?

Sincerely,
Petyo Milotinov
 
in xxxx.h:
//----------------------------------------------------------
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER (WM_ENDSESSION,TWMEndSession,WndProc);
END_MESSAGE_MAP(TForm);
//----------------------------------------------------------
in xxxx.cpp:
//----------------------------------------------------------
void __fastcall TFormTest::WndProc(TWMEndSession& Msg)
{
bForceClose = true;
}
//----------------------------------------------------------
void __fastcall TFormTest::FormClose(TObject *Sender, TCloseAction &Action)
{
if(bForceClose)
return;

// Password dialog...
// Action = caNone;
}
//----------------------------------------------------------

 
I tried this one (which is equivalent):

//in cpp file
void __fastcall TMainForm::WMEndSession(TWMEndSession &Msg)
{
if ( Msg.EndSession == true )
{
bForceClose = true;
Close();
Msg.Result = 1;
}
} */
//---------------------------------------------------------------------------
void __fastcall TMainForm::WMQueryEndSession(TWMQueryEndSession &Msg)
{
bForceClose = true;
Msg.Result = true;
}

//in .h file
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_QUERYENDSESSION, WMQueryEndSession, WMQueryEndSession)
MESSAGE_HANDLER(WM_ENDSESSION, TWMEndSession, WMEndSession)
END_MESSAGE_MAP(TForm)

but the results are the same :-((
 
Remove all references to "EndSession".
EndSession is called after QueryEndSession.
Then It must work!

dr. S.Lector.
 
Thanks for your support, dr. Lector!
I found the solution - I have taken away Borland's OnFormCloseQuery event and I have put everything in WndProc. Here is the code:

void __fastcall TMainForm::WndProc( TMessage &Msg)
{
switch ( Msg.Msg )
{
case WM_ENDSESSION:
if ( (bool)Msg.WParam )
{
//ShowMessage("WM_ENDSESSION");
bForceClose = true;
Msg.Result = true;
Close();
}
break;

case WM_QUERYENDSESSION:
//ShowMessage("WM_QUERYENDSESSION");
bForceClose = true;
Msg.Result = true;
break;

case WM_QUIT:
//ShowMessage("WM_QUIT");
case WM_CLOSE:
//ShowMessage("WM_CLOSE");
if ( bForceClose || Application->Active )
Close();
else
{
try {
PasswordDlg->ShowModal();
}
catch (Exception &E) {}
if ( bCorrectPass )
Close();
}

default: break;
}
inherited::WndProc( Msg);
}

It is working for the present.
Tnanks again for your support!

Sincerely,
Petyo Milotinov
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top