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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SetConsoleCtrlHandler() problem

Status
Not open for further replies.

graetzd

Technical User
Jun 29, 2001
17
US
Hello Forum Gurus:

I am implementing a CONSOLE application vis-a-vis my SDI application. I found some code to do this, and I can get a win-32 style console to launch correctly and send output to the console.

My Problem:
I would like for the console to exit "gracefully" when the user hits the X-button or choses Close from the menu. I do not want my SDI application to terminate, I simply want the console window to go away.

I have been trying to set a console ctrl handler for the CTRL_CLOSE_EVENT and it does not seem to work as I anticipated:

My Code:

/////////////////////////////////
void CSLOMODoc::LaunchConsole()
/////////////////////////////////
{

//Free any console that may already be attached
FreeConsole();

//Allocate a new console
AllocConsole();

//Use my console Handlers
SetConsoleCtrlHandler(MyConsoleHandler, TRUE);
}

BOOL WINAPI MyConsoleHandler(DWORD dwCtrlType)
{
switch(dwCtrlType)
{
case CTRL_C_EVENT:
return TRUE; //this just disables Ctrl-C
case CTRL_CLOSE_EVENT:
FreeConsole();
return TRUE;
default:
return FALSE;
}
}

I have checked to ensure that MyConsoleHandler() is implemented with no errors. And when I debug and hit the X-button I have verified that I enter the CTRL_CLOSE_EVENT case statement. But I was expecting the FreeConsole() call to simply detach the process (my SDI application) from the console and since that is the only process attached to the console it should simply go away. What happens is that the entire SDI application closes up and goes away!

Any suggestions or help?!?!?

Thank you,
David G.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top