SetConsoleCtrlHandler

The SetConsoleCtrlHandler function adds or removes an application-defined HandlerRoutine function from the list of handler functions for the calling process.

Windows NT: If no handler function is specified, the function sets an inheritable attribute that determines whether the calling process ignores ctrl+c signals.

BOOL SetConsoleCtrlHandler(
  PHANDLER_ROUTINE HandlerRoutine,  // address of handler function
  BOOL Add                          // handler to add or remove
);
 

Parameters

HandlerRoutine
Pointer to the application-defined HandlerRoutine function to add or remove.

Windows NT: This parameter can be NULL.

Add
Specifies whether to add or remove the function pointed to by the HandlerRoutine parameter from the handler list. If this parameter is TRUE, the handler is added; if it is FALSE, the handler is removed.

Windows NT: If the HandlerRoutine parameter is NULL, a TRUE value causes the calling process to ignore ctrl+c input, and a FALSE value restores normal processing of ctrl+c input. This attribute of ignoring or processing ctrl+c is inherited by child processes.

Return Values

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

Each console process has its own list of application-defined HandlerRoutine functions that handle ctrl+c and ctrl+break signals. The handler functions also handle signals generated by the system when the user closes the console, logs off, or shuts down the system. Initially, the handler list for each process contains only a default handler function that calls the ExitProcess function. A console process adds or removes additional handler functions by calling the SetConsoleCtrlHandler function, which does not affect the list of handler functions for other processes. When a console process receives any of the control signals, its handler functions are called 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.

For console processes, the ctrl+c and ctrl+break key combinations are typically treated as signals (CTRL_C_EVENT and CTRL_C_BREAK_EVENT). When a console window with the keyboard focus receives ctrl+c or ctrl+break, the signal is typically passed to all processes sharing that console.

ctrl+break is always treated as a signal, but typical ctrl+c behavior can be changed in three ways that prevent the handler functions from being called:

A console process can use the GenerateConsoleCtrlEvent function to send a ctrl+c or ctrl+break signal to a console process group.

The system generates CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, and CTRL_SHUTDOWN_EVENT signals when the user closes the console, logs off, or shuts down the system so that the process has an opportunity to clean up before termination. Console functions, or any C run-time functions that call console functions, may not work reliably during processing of any of the three signals mentioned previously. The reason is that some or all of the internal console cleanup routines may have been called before executing the process signal handler.

QuickInfo

  Windows NT: Requires version 3.1 or later.
  Windows: Requires Windows 95 or later.
  Windows CE: Unsupported.
  Header: Declared in wincon.h.
  Import Library: Use kernel32.lib.

See Also

Consoles and Character-Mode Support Overview, Console Functions, ExitProcess, GenerateConsoleCtrlEvent, GetConsoleMode, HandlerRoutine, SetConsoleMode