SetConsoleCtrlHandler

  BOOL SetConsoleCtrlHandler(pHandlerRoutine, fAdd)    
  PHANDLER_ROUTINE pHandlerRoutine; /* pointer to handler function */
  BOOL fAdd; /* add or remove handler */

The SetConsoleCtrlHandler function is used to add or remove a Ctrl-c and Ctrl-break handler from the list of handlers for the current process.

Parameters

pHandlerRoutine

A pointer to the handler routine to add or remove.

fAdd

Specifies whether the handler routine is to be added or removed from the handler list. If fAdd is TRUE, the handler is added; if fAdd is FALSE, the handler is removed.

Return Value

The return value is TRUE if the function was successful, or FALSE if an error occurred. To obtain extended error information, use the GetLastError function.

Comments

The handler specified by the pHandlerRoutine parameter has the following type definition:

typedef BOOL (*PHANDLER_ROUTINE)(DWORD dwCtrlType);

Member Description

dwCtrlType Specifies the type of event:
Value Meaning

CTRL_C_EVENT A Ctrl-c event was input.
CTRL_BREAK_EVENT A Ctrl-break event was input.

The handler routine returns a Boolean result. If TRUE, it indicates that the control character was handled and other handlers should not be invoked.

SetConsoleCtrlHandler adds or removes a control handler from the per process list of handlers to be called when a Ctrl-c or Ctrl-break event is input. The handler functions will always be invoked if Ctrl-break is input; but Ctrl-C is only passed through if the console is in processed input mode (ENABLE_PROCESSED_INPUT).

If no handlers are registered for a process, a default handler that calls ExitProcess is called. If multiple handlers have been registered, they 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.