Console Control Handlers
Exception handlers can respond to a variety of events, but they do not detect situ-
ations such a s the user logging off or entering a
from the keyboard to stop
a program. Console control handlers are required to detect such events.
The function
allows one or more specified func-
tions to be executed on receipt of a
, , or one of three other
console-related signals. The
function, described in
Chapter 6, also generates these signals, and the signals can be sent to other pro-
cesses that are sharing the same console. The hand lers are user-specified Boolean
functions that tak e a
argument identifying the actual signal.
Multiple handlers can be associated with a signal, and handlers can be
removed as well as added. Here is the function used to add or delete a handler.
The handler routine is ad d ed if the
flag is ; otherwise, it is deleted
from the list of console control routines. Notice that the actual signal is not
specified . The handler must test to see which signal was received .
The actua l ha ndler routine returns a Boolean value and takes a single
parameter that identifies the actual signal. The handler name in the definition is
a placeholder; the programmer specifies the name.
Here are some other considerations when using console control handlers.
•If the
parameter is and is , signals
will be ignored.
flag on (Chapter 2) will
to be treated as keyboard input rather than as a signal.
•The handler routine actually executes as an independent threa d (see
Chapter 7) within the process. The norma l program will continue to operate,
as shown in the next example.
•Raising an exception in the handler will not cause
an exception in the thread
that was interrupted because exceptions apply to threads, not to an entire pro-
cess. If you wish to communicate with the interrupted thread, use a variable,
as in the next example, or a synchroniza tion method (Chapter 8).
There is one other important di stinction between exceptions and si
gnals. A
signal applies to the entire process, whereas an exception applies only to the
thread executing the code where the exception occurs.
identifies the actual signal (or event) and can take on one of the
following five values.
indicates that the sequence was entered from the
indicates that the console window is being closed.
indicates the signal.
indicates that the user is logging off.
indicates that the system is shutting down.
The signal handler can perform cleanup operations just as an exception or ter-
mination handler would. The signal handler should return
to indicate that
the function handled the signal. If the si gnal handler returns
, the next
handler function in the list is executed. The signal handlers are executed in the
reverse order from the way they were set, so that the most recently set handler is
executed first and the system handler is executed last.

Get Windows System Programming Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.