BUG: Queued Console Control Signals May be Missed Under Win95

ID: Q134284

The information in this article applies to:

SYMPTOMS

Console applications call SetConsoleCtrlHandler() to install or remove application-defined callback functions to handle console control signals. Typically a console window with keyboard focus receives the CTRL_C_EVENT and CTRL_BREAK_EVENT signals when the CTRL+C and CTRL+BREAK keys are pressed or if they are generated using the GenerateConsoleCtrlEvent() function. But 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 console process has an opportunity to clean up before termination.

In Windows 95, console applications that install console control handler functions by using SetConsoleCtrlHandler() do not always get all console signals when two or more signals occur at almost the same time.

Note that this problem does not occur under Windows NT versions 3.51 and 4.0.

CAUSE

The Windows 95 console system does not queue up console control signals or events. If multiple events occur rapidly in succession, events received later overwrite those received earlier, resulting in the earlier events being lost. The number of events that the console application receives depends on when the events actually arrive; the shorter the interval between the events, the more likely that one or more will be lost.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

The following code demonstrates the problem. It should receive five CTRL_C_EVENT and five CTRL_BREAK_EVENT events, and print a line for each. However, because events are not queued, it will print less than five of each event type, and may print only the last CTRL_BREAK_EVENT.

Sample Code to Demonstrate Problem

 // Console Application
 #include <windows.h>
 #include <stdio.h>

 BOOL WINAPI CtrlHandler (DWORD dwEvent);

 void main (void)
 {
    printf ("Installing handler\n");
    SetConsoleCtrlHandler (CtrlHandler, TRUE);

    GenerateConsoleCtrlEvent (CTRL_C_EVENT, 0);
    GenerateConsoleCtrlEvent (CTRL_C_EVENT, 0);
    GenerateConsoleCtrlEvent (CTRL_C_EVENT, 0);
    GenerateConsoleCtrlEvent (CTRL_C_EVENT, 0);
    GenerateConsoleCtrlEvent (CTRL_C_EVENT, 0);

    GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, 0);
    GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, 0);
    GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, 0);
    GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, 0);
    GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, 0);

    printf ("Removing handler\n");
    SetConsoleCtrlHandler (CtrlHandler, FALSE);
 }

 BOOL WINAPI CtrlHandler (DWORD dwEvent)
 {
    switch (dwEvent)
       {
       case CTRL_C_EVENT:
          printf("Got CTRL_C_EVENT\n");
          break;

       case CTRL_BREAK_EVENT:
          printf("Got CTRL_BREAK_EVENT\n");
          break;

       case CTRL_LOGOFF_EVENT:
          printf("Got CTRL_LOGOFF_EVENT\n");
          break;

       case CTRL_SHUTDOWN_EVENT:
          printf("Got CTRL_SHUTDOWN_EVENT\n");
          break;

       case CTRL_CLOSE_EVENT:
          printf("Got CTRL_CLOSE_EVENT\n");
          break;

       default:
          // unknown type--better pass it on.
          return (FALSE);
       }
    // Handled all known events
    return (TRUE);
 }

Additional query words: Windows 95 win95
Keywords          : kbprg kbConsole kbKernBase kbGrpKernBase kbbuglist
Issue type        : kbbug

Last Reviewed: March 7, 1997