Console message generation and Processing

Source: Internet
Author: User

Friends who have learned window programs will know the concept of messages and how to process messages (system messages or custom messages). However, our console programs also have messages, let's take a look at the generation and processing of messages in the console program:

First, I will introduce two functions:

Setconsolectrlhandler:

Bool winapi setconsolectrlhandler (

_ In phandler_routineHandlerroutine,

_ In boolAdd

);

This function is used to define the CTRL message processing function.

Generateconsolectrlevent:

Bool winapi generateconsolectrlevent (_ in DWORDDwctrlevent, _ In DWORDDwprocessgroupid);

This function is used to generate messages.

For more information about these two functions, refer to csdn. Let's take a look at the simple use of these two functions.

 

# Include <stdio. h>

# Include <windows. h>

Bool winapi handlerroutine (DWORD dwctrltype );

Int main (INT argc, char * argv [])

{

Setconsolectrlhandler (handlerroutine, true );

 

While (1 );

Return 0;

}

Bool winapi handlerroutine (DWORD dwctrltype)

{

Switch (dwctrltype)

{

Case ctrl_c_event:

Printf ("the user pressed Ctrl + C! /N ");

Break;

Case ctrl_break_event:

Printf ("the user pressed Ctrl + break! /N ");

Break;

Case ctrl_close_event:

Printf ("You have disabled the console (disabled menu or from Task Manager )! /N ");

Break;

Case ctrl_logoff_event:

Printf ("a user exits! /N ");

Break;

Case ctrl_shutdown_event:

Printf ("the operating system is disabled! /N ");

Break;

Default:

Return false;

Break;

}

Return true;

}

 

This program is used to capture and process user messages. There are only five types of messages that can be captured here.

1 Ctrl + C 2 Ctrl + break 3 user shut down console 4 one user quit 5 user shut down Operating System

 

Next let's take a look at how to generate console messages, that is, we send messages to the console in the program.

Generateconsolectrlevent (ctrl_c_event, 0 );

In the above program code, add the above Code and run the program. Then we can see the running result. The above code is used to send a Ctrl + C message to the console program.

 

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.