ENTERPRISE

Windows System Programming : Exception Handling - Example: A Console Control Handler

1/30/2015 1:09:21 AM

Program 1 loops forever, calling the self-explanatory Beep function every 5 seconds. The user can terminate the program with a Ctrl-C or by closing the console. The handler routine will put out a message, wait 10 seconds, and, it would appear, return TRUE, terminating the program. The main program, however, detects the exitFlag flag and stops the process. This illustrates the concurrent operation of the handler routine; note that the timing of the signal determines the extent of the signal handler’s output. 

Program 1. Ctrlc: Signal Handling Program
#include "Everything.h"static BOOL WINAPI Handler(DWORD cntrlEvent);static BOOL exitFlag = FALSE;int _tmain(int argc, LPTSTR argv[])/* Beep periodically until signaled to stop. */{   /* Add an event handler. */   SetConsoleCtrlHandler(Handler, TRUE);   while (!exitFlag) {      Sleep(5000); /* Beep every 5 seconds. */      Beep(1000 /* Frequency. */, 250 /* Duration. */);   }   _tprintf(_T("Stopping the program as requested.\n"));   return 0;}BOOL WINAPI Handler(DWORD cntrlEvent){   exitFlag = TRUE;   switch (cntrlEvent) {      /* Timing determines if you see the second handler message. */      case CTRL_C_EVENT:         _tprintf(_T("Ctrl-C. Leaving in <= 5 seconds.\n"));         exitFlag = TRUE;         Sleep(4000); /* Decrease to get a different effect */         _tprintf(_T("Leaving handler in 1 second or less.\n"));         return TRUE; /* TRUE indicates the signal was handled. */      case CTRL_CLOSE_EVENT:         _tprintf(_T("Close event. Leaving in <= 5 seconds.\n"));         exitFlag = TRUE;         Sleep(4000); /* Decrease to get a different effect */         _tprintf(_T("Leaving handler in <= 1 second.\n"));         return TRUE; /* Try returning FALSE. Any difference? */     default:        _tprintf(_T("Event: %d. Leaving in <= 5 seconds.\n"),           cntrlEvent);        exitFlag = TRUE;        Sleep(4000); /* Decrease to get a different effect */        _tprintf(_T("Leaving handler in <= 1 second.\n"));        return TRUE; /* TRUE indicates the signal was handled. */   }}					  

Note the use of WINAPI; this macro is for user functions passed as arguments to Windows functions to assure the proper calling conventions. It is defined in the Platform SDK header file windef.h.

There’s very little to show with this program, as we can’t show the sound effects. Nonetheless, Run 4-5 shows the command window where I typed Ctrl-C after about 11 seconds.

Other  
  •  Windows System Programming : Exception Handling - Console Control Handlers
  •  Microsoft Enterprise Library : Relieving Cryptography Complexity - Diving in with an Example (part 2) - Obtaining and Comparing Hash Values
  •  Microsoft Enterprise Library : Relieving Cryptography Complexity - Diving in with an Example (part 1) - Encrypting and Decrypting Data Using A Symmetric Provider
  •  Microsoft Enterprise Library : Relieving Cryptography Complexity - How Do I Use the Cryptography Block?
  •  Microsoft Enterprise Library : Relieving Cryptography Complexity - What Does the Cryptography Block Do?
  •  Review : Aztech FG7008GR(AC) 2400Mbps Gigabit Router
  •  Using Exchange Server 2010 Antispam Tools (part 6) - Sender Reputation
  •  Using Exchange Server 2010 Antispam Tools (part 5) - Sender Filtering, Sender ID
  •  Using Exchange Server 2010 Antispam Tools (part 4) - IP Block and Allow Lists, Recipient Filtering , Tarpitting
  •  Using Exchange Server 2010 Antispam Tools (part 3) - IP Block and IP Allow Providers
  •  
    Video
    Video tutorials
    - How To Install Windows 8

    - How To Install Windows Server 2012

    - How To Install Windows Server 2012 On VirtualBox

    - How To Disable Windows 8 Metro UI

    - How To Install Windows Store Apps From Windows 8 Classic Desktop

    - How To Disable Windows Update in Windows 8

    - How To Disable Windows 8 Metro UI

    - How To Add Widgets To Windows 8 Lock Screen

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010
    programming4us programming4us
    Top 10
    Free Mobile And Desktop Apps For Accessing Restricted Websites
    MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
    TOYOTA CAMRY 2; 2.5 : Camry now more comely
    KIA SORENTO 2.2CRDi : Fuel-sipping slugger
    How To Setup, Password Protect & Encrypt Wireless Internet Connection
    Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
    Backup & Restore Game Progress From Any Game With SaveGameProgress
    Generate A Facebook Timeline Cover Using A Free App
    New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    Popular Tags
    Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Exchange Server Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe Photoshop CorelDRAW X5 CorelDraw 10 windows Phone 7 windows Phone 8 Iphone