|
|
1.1.1.3 ! root 1: ! 2: /******************************************************************************\ ! 3: * This is a part of the Microsoft Source Code Samples. ! 4: * Copyright (C) 1993 Microsoft Corporation. ! 5: * All rights reserved. ! 6: * This source code is only intended as a supplement to ! 7: * Microsoft Development Tools and/or WinHelp documentation. ! 8: * See these sources for detailed information regarding the ! 9: * Microsoft samples programs. ! 10: \******************************************************************************/ ! 11: 1.1 root 12: #include <windows.h> 13: #include <string.h> 14: #include "console.h" 15: 16: HANDLE hConsole; /* current console output handle */ 17: 18: /******************************************************************* 19: * FUNCTION: handler_routine(DWORD dwCtrlType) * 20: * * 21: * PURPOSE: this is the control handler routine activated by ctrl+c * 22: * or ctrl+break keys * 23: * * 24: * INPUT: the type of control event * 25: *******************************************************************/ 26: 1.1.1.2 root 27: BOOL WINAPI handler_routine(DWORD dwCtrlType) 1.1 root 28: { 29: CHAR szTemp[64]; 30: 31: /* print out what control event was received to the current console */ 32: switch(dwCtrlType) 33: { 34: case CTRL_C_EVENT: 35: strcpy(szTemp, "CTRL_C_EVENT"); 36: break; 37: case CTRL_BREAK_EVENT: 38: strcpy(szTemp, "CTRL_BREAK_EVENT"); 39: break; 1.1.1.3 ! root 40: case CTRL_CLOSE_EVENT: ! 41: strcpy(szTemp, "CTRL_CLOSE_EVENT"); ! 42: break; ! 43: case CTRL_LOGOFF_EVENT: ! 44: strcpy(szTemp, "CTRL_LOGOFF_EVENT"); ! 45: break; ! 46: case CTRL_SHUTDOWN_EVENT: ! 47: strcpy(szTemp, "CTRL_SHUTDOWN_EVENT"); ! 48: break; 1.1 root 49: default: 50: strcpy(szTemp, "unknown event"); 51: break; 52: } 53: strcat(szTemp, " detected"); 54: myPuts(hConsole, szTemp); 55: return(TRUE); 56: } 57: 58: 59: /********************************************************************* 60: * FUNCTION: demoSetCtrlHandler(HANDLE hConOut) * 61: * * 62: * PURPOSE: demonstrate SetConsoleCtrlHandler by setting a ctrl+break * 63: * and ctrl+c handler. When the user hits either one of * 64: * these keys, a message is printed to the console * 65: * indicating the event. * 66: * * 67: * INPUT: the output handle to write to * 68: *********************************************************************/ 69: 70: void demoSetCtrlHandler(HANDLE hConOut) 71: { 72: BOOL bSuccess; 73: 74: setConTitle(__FILE__); 75: hConsole = hConOut; /* set global console output handle for handler */ 76: myPuts(hConOut, "Let's install a ctrl+c and ctrl+break handler for this\n" 77: "process. Hit ctrl+c and ctrl+break a few times to test\n" 78: "the handler. Hit enter to return..."); 79: /* set handler for this process */ 80: bSuccess = SetConsoleCtrlHandler(handler_routine, TRUE); 81: PERR(bSuccess, "SetConsoleCtrlHandler"); 82: /* wait for user to hit enter */ 83: while (myGetchar() != 0xd) 84: ; 1.1.1.3 ! root 85: /* now let's generate some control events */ ! 86: myPuts(hConOut, "Now we'll use GenerateConsoleCtrlEvent to generate a\n" ! 87: "ctrl+c and a ctrl+break event...\n"); ! 88: bSuccess = GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0); ! 89: PERR(bSuccess, "GenerateConsoleCtrlEvent"); ! 90: bSuccess = GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, 0); ! 91: PERR(bSuccess, "GenerateConsoleCtrlEvent"); ! 92: Sleep(1000); /* give ctrl handle time to output messages */ ! 93: myPuts(hConOut, "\nNow choose 'Close' then 'Cancel' from the system\n" ! 94: "menu of this console and note that we receive a\n" ! 95: "CTRL_CLOSE_EVENT...\n"); ! 96: myPuts(hConOut, "\nHit enter to continue..."); ! 97: myGetchar(); 1.1 root 98: /* remove our handler from the list of handlers */ 99: bSuccess = SetConsoleCtrlHandler(handler_routine, FALSE); 100: PERR(bSuccess, "SetConsoleCtrlHandler"); 101: return; 102: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.