Archived

This topic is now archived and is closed to further replies.

Stopping ctr-c from closing a console application

This topic is 5012 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m writing a program and I want to use ctr-c to copy text to the clipboard. Yet ctr-c is the shell command to "kill" a console application. Does anyone know how to stop it from closing the console window? Thank you, Dave

Share this post


Link to post
Share on other sites
You''ll need to make a few significant changes, the first being the use of the Win32 API:

#include <windows.h>
 
int WINAPI WinMain( HINSTANCE, HINSTANCE, LPCTSTR, int )
{
// explicitly create a console window
AllocConsole()
 
// install console control handler
SetConsoleCtrlHandler( myHandler, TRUE );
 
// explicitly release a console window
FreeConsole()
return 0;
}
The myHandler function is a HandlerRoutine that would look like this to handle Ctrl+C events only:
BOOL WINAPI myHandler( DWORD ctrlType )
{
if( ctrlType == CTRL_C_EVENT )
{
// copy buffer contents to clipboard
return TRUE;
}
else return FALSE;
}

Resources:
Win32 Console Functions
SetConsoleControlHandler
HandlerRoutine
Registering a Console Handler Function

Share this post


Link to post
Share on other sites