Console Control Handlers

Each console process has its own list of control handler functions that are called by the system when the process receives a CTRL+C, CTRL+BREAK, or CTRL+CLOSE signal. Initially, the list of control handlers for each process contains only a default handler function that calls the ExitProcess function. A console process can add or remove additional HandlerRoutine functions by calling the SetConsoleCtrlHandler function. This function does not affect the lists of control handlers for other processes. When a console process receives any of the control signals, it calls the handler functions on a last-registered, first-called basis until one of the handlers returns TRUE. If none of the handlers returns TRUE, the default handler is called.

The function's dwCtrlType parameter identifies which control signal was received, and the return value indicates whether the signal was handled.

A new thread is started inside the command-line client process to run the handler routines. More information on the timeout values and action of this thread can be found in the HandlerRoutine function documentation.

For an example of a control handler function, see Registering a Control Handler Function.

Note that calling AttachConsole, AllocConsole, or FreeConsole will reset the table of control handlers in the client process to its initial state.