Thread View: borland.public.cppbuilder.nativeapi
2 messages
2 total messages
Started by Mike Collins
Wed, 08 Aug 2007 12:11
SetConsoleCtrlHandler not processing close messages
Author: Mike Collins
Date: Wed, 08 Aug 2007 12:11
Date: Wed, 08 Aug 2007 12:11
85 lines
3564 bytes
3564 bytes
Hi all, posted a few days ago about a console application i was writing,
specifically processing messages within. I found some code that remy had
posted and tried to impliment it. The code seems to work but when i hit
Ctrl+Break the console application does not terminate. The
SetConsoleCtrlHandler callback seems to be called and the PostQuitMessage()
is sent but the console app does not terminate. Thus i can't find any way
to drop out of the message processing and my console app does not clean up
after it's self. My code looks like this:
BOOL CALLBACK HandlerRoutine(DWORD dwCtrlType)
{
PostQuitMessage(0);
return TRUE;
}
//---------------------------------------------------------------------------
int main(int argc, char* argv[])
{
if ((argc > 1)&&(stricmp(argv[1], "/?") == 0))
{
// Show help options
...
}
else
{
int iPort = DEF_SERVER_PORT_NO;
int iServerType = DEF_SERVER_TYPE;
int iThreadCache = DEF_THREAD_CACHE_COUNT;
int iReturnErr = DEF_ERROR_RETURN_VALUE;
AnsiString szExponent, szModulus;
int iRtn = 0;
TWebMod *WebMod = NULL;
try
{
try
{
SetConsoleCtrlHandler(&HandlerRoutine, TRUE);
WebMod = new TWebMod(NULL);
WebMod->InitialiseSocket(iPort, iServerType, iThreadCache,
szExponent, szModulus);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0) //&&(!bEnd))//&&(ch
!= 'x'))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
switch (msg.message)
{
case WM_SYSCOMMAND : if ((msg.wParam & 0xFFF0) !=
SC_CLOSE)
break;
// fall though to the next
block...
case WM_CLOSE : if (PeekMessage(&msg, NULL,
WM_QUIT, WM_QUIT, PM_NOREMOVE) == 0)
PostQuitMessage(0);
break;
}
}
}
catch (...)
{
printf("\t\t\t ERROR");
iRtn = iReturnErr;
}
}
__finally
{
if (WebMod)
{
delete WebMod;
WebMod = NULL;
}
SetConsoleCtrlHandler(&HandlerRoutine, FALSE);
}
return iRtn;
}
}
Any pointers would be greatfully recieved.
Mike C
Re: SetConsoleCtrlHandler not processing close messages
Author: Bob Gonder
Date: Thu, 09 Aug 2007 14:41
Date: Thu, 09 Aug 2007 14:41
67 lines
2945 bytes
2945 bytes
Mike Collins wrote:
>Hi all, posted a few days ago about a console application i was writing,
>specifically processing messages within.
I don't use a message loop in my console app, but this is how I handle the handler.
static void EmergencyShutdown(void) /* we are getting out of here the hard way */
{ throw_away_keys(); /* toss any queued keys */
#if UDP
if( UDPRunning )
KillUDP(); /* up to 5 sec delay */
#endif
AppCanceled = TRUE;
UnwindLockQue();
UnwindFileQue();
UnwindManagedIndexes();
/* remove myself from the handler queue */
SetConsoleCtrlHandler( &CtrlHandler, FALSE);
}
static BOOL _stdcall CtrlHandler(DWORD fdwCtrlType)
{
switch (fdwCtrlType)
{
/* Handle the CTRL+C signal. */
case CTRL_C_EVENT:
push_key(CtrlC); /* pass Ctrl-C to app */
return TRUE;
/* CTRL+CLOSE: confirm that the user wants to exit. */
/* Ctrl_Close _needs_ to return False because I don't want a dialog poping up.
Other events return True because I handled them this time,
and disabled myself so subsequent events will be false.
*/
case CTRL_CLOSE_EVENT:
case CTRL_BREAK_EVENT:
EmergencyShutdown();
return FALSE; // next handler terminates me
/* Pass other signals to the next handler. */
/* Next handler will pop a terminate dialog */
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
EmergencyShutdown();
return TRUE;
default:
return FALSE;
};
}
Then, all sub-threads, such as a watchdog timer, check AppCanceled.
The application itself isn't going to run after I return.
> The code seems to work but when i hit
>Ctrl+Break the console application does not terminate.
Thanks, mine wasn't working either, then I switched it to FALSE
(see above), it terminated fine.
It could well be that because you don't discriminate in the handler,
that you enter an endless PostQuit-Close loop.
I would suggest turning off the handler within the handler.
That would eliminate the looping.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads