programming4us
programming4us
ENTERPRISE

Windows System Programming : Exception Handling - Console Control Handlers

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

Exception handlers can respond to a variety of asynchronous events, but they do not detect situations such as the user logging off or entering a Ctrl-C from the keyboard to stop a program. Use console control handlers to detect such events.

The function SetConsoleCtrlHandler allows one or more specified functions to be executed on receipt of a Ctrl-C, Ctrl-break, or one of three other console-related signals. The GenerateConsoleCtrlEvent function, also generates these signals, and the signals can be sent to other processes that are sharing the same console. The handlers are user-specified Boolean functions that take a DWORD argument identifying the signal.

Multiple handlers can be associated with a signal, and handlers can be removed as well as added. Here is the function to add or delete a handler.

BOOL SetConsoleCtrlHandler (   PHANDLER_ROUTINE HandlerRoutine,   BOOL Add)


The handler routine is added if the Add flag is TRUE; otherwise, it is deleted from the list of console control routines. Notice that the signal is not specified. The handler must test to see which signal was received.

The handler routine returns a Boolean value and takes a single DWORD parameter that identifies the signal. The HandlerRoutine in the definition is a placeholder; the programmer specifies the name.

Here are some other considerations when using console control handlers.

  • If the HandlerRoutine parameter is NULL and Add is TRUE, Ctrl-C signals will be ignored.

  • The ENABLE_PROCESSED_INPUT flag on SetConsoleMode will cause Ctrl-C to be treated as keyboard input rather than as a signal.

  • The handler routine actually executes as an independent thread within the process. The normal 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 process. If you wish to communicate with the interrupted thread, use a variable, as in the next example, or a synchronization method.

There is one other important distinction between exceptions and signals. A signal applies to the entire process, whereas an exception applies only to the thread executing the code where the exception occurs.

BOOL HandlerRoutine (DWORD dwCtrlType)


dwCtrlType identifies the signal (or event) and can take on one of the following five values.

  1. CTRL_C_EVENT indicates that the Ctrl-C sequence was entered from the keyboard.

  2. CTRL_CLOSE_EVENT indicates that the console window is being closed.

  3. CTRL_BREAK_EVENT indicates the Ctrl-break signal.

  4. CTRL_LOGOFF_EVENT indicates that the user is logging off.

  5. CTRL_SHUTDOWN_EVENT indicates that Windows is shutting down.

The signal handler can perform cleanup operations just as an exception or termination handler would. The signal handler can return TRUE to indicate that the function handled the signal. If the signal handler returns FALSE, 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.

Other  
  •  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
  •  Using Exchange Server 2010 Antispam Tools (part 2) - Content Filtering
  •  
    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
    - Messages forwarded by Outlook rule go nowhere
    - Create and Deploy Windows 7 Image
    - How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
    - Creating and using an unencrypted cookie in ASP.NET
    - Directories
    - Poor Performance on Sharepoint 2010 Server
    - SBS 2008 ~ The e-mail alias already exists...
    - Public to Private IP - DNS Changes
    - Send Email from Winform application
    - How to create a .mdb file from ms sql server database.......
    programming4us programming4us
    programming4us
     
     
    programming4us