Stopping ctr-c from closing a console application

Started by
0 comments, last by ph33r 20 years, 1 month ago
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
Advertisement
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

This topic is closed to new replies.

Advertisement