SetConsoleCtrlHandler Handling Console messages

Source: Internet
Author: User

SetConsoleCtrlHandler Handling Console messages


First, how to handle all console messages.


The first step is to install an event hook, which means to create a callback function. Invoke the Win32 API, the prototype is as follows:


BOOL SetConsoleCtrlHandler (

Phandler_routine Handlerroutine,//callback function

BOOL Add//= Add or remove

);


The parameter handlerroutine is a pointer to a function, and the prototype is as follows:


BOOL WINAPI Handlerroutine (

DWORD Dwctrltype//control event Type

);


All the Handlerroutine functions have only one parameter dwctrltype, and he indicates what message the console sent. Parameter has the following values:


Ctrl_c_event-When the user presses CTRL + C or is issued by the Generateconsolectrlevent API.

Ctrl_break_event-The user presses the Ctrl+break or is issued by the Generateconsolectrlevent API.

Ctrl_close_event-When attempting to close the console program, the system sends a shutdown message.

Ctrl_logoff_event-When the user exits, but cannot determine which user.

Ctrl_shutdown_event-When the system is shut down.


When an event is received, Handlerroutine can choose to handle it or simply ignore it. If the callback function chooses to ignore, the function returns False, and the next hook program is processed by the system. If the message is processed, the program should return true after processing the message.





Ctrl_close_event, Ctrl_logoff_event, and ctrl_shutdown_event are often used to process cleanup of some programs, and then invoke the ExitProcess API. In addition, these three events have a time-out mechanism, ctrl_close_event is 5 seconds, and the other two are 20 seconds. If the program is over time, the system will pop up the dialog box for the end process. If the user chooses to end the process, no cleanup will be done, so the work should be done within the timeout period. The following is an example of a callback function:


BOOL WINAPI Consolehandler (DWORD CEvent)

{

Char mesg[128];


Switch (CEvent)

{

Case Ctrl_c_event:

MessageBox (NULL,

"Ctrl + C received!", "CEvent", MB_OK);

Break

Case Ctrl_break_event:

MessageBox (NULL,

"Ctrl+break received!", "CEvent", MB_OK);

Break

Case Ctrl_close_event:

MessageBox (NULL,

"Program being closed!", "CEvent", MB_OK);

Break

Case Ctrl_logoff_event:

MessageBox (NULL,

"User is logging off!", "CEvent", MB_OK);

Break

Case Ctrl_shutdown_event:

MessageBox (NULL,

"User is logging off!", "CEvent", MB_OK);

Break


}

return TRUE;

}



OK, now that you have the callback function, let's see how to install the hook:


if (SetConsoleCtrlHandler (

(phandler_routine) consolehandler,true) ==false)

{

Unable to install handler ...

Display message to the user

printf ("Unable to install handler!\n");

return-1;

}


The first argument is the function pointer, which is the function above. The second parameter is the flag, and if true, the hook is installed, and if False, the hook is deleted.


OK, after installing the hook, we can receive the console message and remove the hook before the program exits. It's very simple.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.