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.