Annotation of mstools/samples/console/handler.c, revision 1.1.1.1

1.1       root        1: #include <windows.h>
                      2: #include <string.h>
                      3: #include "console.h"
                      4: 
                      5: HANDLE hConsole; /* current console output handle */
                      6: 
                      7: /*******************************************************************
                      8: * FUNCTION: handler_routine(DWORD dwCtrlType)                      *
                      9: *                                                                  *
                     10: * PURPOSE: this is the control handler routine activated by ctrl+c *
                     11: *          or ctrl+break keys                                      *
                     12: *                                                                  *
                     13: * INPUT: the type of control event                                 *
                     14: *******************************************************************/
                     15: 
                     16: BOOL handler_routine(DWORD dwCtrlType)
                     17: {
                     18:   CHAR szTemp[64];
                     19: 
                     20:   /* print out what control event was received to the current console */
                     21:   switch(dwCtrlType)
                     22:     {
                     23:     case CTRL_C_EVENT:
                     24:       strcpy(szTemp, "CTRL_C_EVENT");
                     25:       break;
                     26:     case CTRL_BREAK_EVENT:
                     27:       strcpy(szTemp, "CTRL_BREAK_EVENT");
                     28:       break;
                     29:     default:
                     30:       strcpy(szTemp, "unknown event");
                     31:       break;
                     32:     }
                     33:   strcat(szTemp, " detected");
                     34:   myPuts(hConsole, szTemp);
                     35:   return(TRUE);
                     36: }
                     37: 
                     38: 
                     39: /*********************************************************************
                     40: * FUNCTION: demoSetCtrlHandler(HANDLE hConOut)                       *
                     41: *                                                                    *
                     42: * PURPOSE: demonstrate SetConsoleCtrlHandler by setting a ctrl+break *
                     43: *          and ctrl+c handler. When the user hits either one of      *
                     44: *          these keys, a message is printed to the console           *
                     45: *          indicating the event.                                     *
                     46: *                                                                    *
                     47: * INPUT: the output handle to write to                               *
                     48: *********************************************************************/
                     49: 
                     50: void demoSetCtrlHandler(HANDLE hConOut)
                     51: {
                     52:   BOOL bSuccess;
                     53: 
                     54:   setConTitle(__FILE__);
                     55:   hConsole = hConOut; /* set global console output handle for handler */
                     56:   myPuts(hConOut, "Let's install a ctrl+c and ctrl+break handler for this\n"
                     57:                   "process. Hit ctrl+c and ctrl+break a few times to test\n"
                     58:                   "the handler. Hit enter to return...");
                     59:   /* set handler for this process */
                     60:   bSuccess = SetConsoleCtrlHandler(handler_routine, TRUE);
                     61:   PERR(bSuccess, "SetConsoleCtrlHandler");
                     62:   /* wait for user to hit enter */
                     63:   while (myGetchar() != 0xd)
                     64:     ;
                     65:   /* remove our handler from the list of handlers */
                     66:   bSuccess = SetConsoleCtrlHandler(handler_routine, FALSE);
                     67:   PERR(bSuccess, "SetConsoleCtrlHandler");
                     68:   return;
                     69: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.