Excerpt:

Registering a Control Handler Function

 

This is an example of the SetConsoleCtrlHandler function that is used to install a control handler.

When a CTRL+C signal is received, the control handler returns TRUE, indicating that it has handled the signal. Doing this prevents other control handlers from being called.

When a CTRL_CLOSE_EVENT signal is received, the control handler returns TRUE and the process terminates.

When a CTRL_BREAK_EVENTCTRL_LOGOFF_EVENT, or CTRL_SHUTDOWN_EVENT signal is received, the control handler returns FALSE. Doing this causes the signal to be passed to the next control handler function. If no other control handlers have been registered or none of the registered handlers returns TRUE, the default handler will be used, resulting in the process being terminated.

 
#include <windows.h>
#include <stdio.h> BOOL CtrlHandler( DWORD fdwCtrlType )
{
switch( fdwCtrlType )
{
// Handle the CTRL-C signal.
case CTRL_C_EVENT:
printf( "Ctrl-C event\n\n" );
Beep( 750, 300 );
return( TRUE ); // CTRL-CLOSE: confirm that the user wants to exit.
case CTRL_CLOSE_EVENT:
Beep( 600, 200 );
printf( "Ctrl-Close event\n\n" );
return( TRUE ); // Pass other signals to the next handler.
case CTRL_BREAK_EVENT:
Beep( 900, 200 );
printf( "Ctrl-Break event\n\n" );
return FALSE; case CTRL_LOGOFF_EVENT:
Beep( 1000, 200 );
printf( "Ctrl-Logoff event\n\n" );
return FALSE; case CTRL_SHUTDOWN_EVENT:
Beep( 750, 500 );
printf( "Ctrl-Shutdown event\n\n" );
return FALSE; default:
return FALSE;
}
} int main( void )
{
if( SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, TRUE ) )
{
printf( "\nThe Control Handler is installed.\n" );
printf( "\n -- Now try pressing Ctrl+C or Ctrl+Break, or" );
printf( "\n try logging off or closing the console...\n" );
printf( "\n(...waiting in a loop for events...)\n\n" ); while( 1 ){ }
}
else
{
printf( "\nERROR: Could not set control handler");
return 1;
}
return 0;
}

HandlerRoutine callback function

 

An application-defined function used with the SetConsoleCtrlHandler function. A console process uses this function to handle control signals received by the process. When the signal is received, the system creates a new thread in the process to execute the function.

The PHANDLER_ROUTINE type defines a pointer to this callback function. HandlerRoutine is a placeholder for the application-defined function name.

Syntax

 
BOOL WINAPI HandlerRoutine(
_In_  DWORD dwCtrlType
);

Parameters

dwCtrlType [in]

The type of control signal received by the handler. This parameter can be one of the following values.

Value Meaning
CTRL_C_EVENT
0

A CTRL+C signal was received, either from keyboard input or from a signal generated by the GenerateConsoleCtrlEvent function.

CTRL_BREAK_EVENT
1

A CTRL+BREAK signal was received, either from keyboard input or from a signal generated by GenerateConsoleCtrlEvent.

CTRL_CLOSE_EVENT
2

A signal that the system sends to all processes attached to a console when the user closes the console (either by clicking Close on the console window's window menu, or by clicking the End Task button command from Task Manager  (Test Failed)).

CTRL_LOGOFF_EVENT
5

A signal that the system sends to all console processes when a user is logging off. This signal does not indicate which user is logging off, so no assumptions can be made.

Note that this signal is received only by services. Interactive applications are terminated at logoff, so they are not present when the system sends this signal.

CTRL_SHUTDOWN_EVENT
6

A signal that the system sends when the system is shutting down. Interactive applications are not present by the time the system sends this signal, therefore it can be received only be services in this situation. Services also have their own notification mechanism for shutdown events. For more information, see Handler.

This signal can also be generated by an application usingGenerateConsoleCtrlEvent.

Return value

If the function handles the control signal, it should return TRUE. If it returns FALSE, the next handler function in the list of handlers for this process is used.

Remarks

Because the system creates a new thread in the process to execute the handler function, it is possible that the handler function will be terminated by another thread in the process. Be sure to synchronize threads in the process with the thread for the handler function.

Each console process has its own list of HandlerRoutine functions. Initially, this list contains only a default handler function that calls ExitProcess. A console process adds or removes additional handler functions by calling the SetConsoleCtrlHandler function, which does not affect the list of handler functions for other processes. When a console process receives any of the control signals, its handler functions are called on a last-registered, first-called basis until one of the handlers returns TRUE. If none of the handlers returns TRUE, the default handler is called.

The CTRL_CLOSE_EVENTCTRL_LOGOFF_EVENT, and CTRL_SHUTDOWN_EVENT signals give the process an opportunity to clean up before termination. A HandlerRoutine can perform any necessary cleanup, then take one of the following actions:

  • Call the ExitProcess function to terminate the process.
  • Return FALSE. If none of the registered handler functions returns TRUE, the default handler terminates the process.
  • Return TRUE. In this case, no other handler functions are called and the system terminates the process.

A process can use the SetProcessShutdownParameters function to prevent the system from displaying a dialog box to the user during logoff or shutdown. In this case, the system terminates the process whenHandlerRoutine returns TRUE or when the time-out period elapses.

When a console application is run as a service, it receives a modified default console control handler. This modified handler does not call ExitProcess when processing the CTRL_LOGOFF_EVENT andCTRL_SHUTDOWN_EVENT signals. This allows the service to continue running after the user logs off. If the service installs its own console control handler, this handler is called before the default handler. If the installed handler calls ExitProcess when processing the CTRL_LOGOFF_EVENT signal, the service exits when the user logs off.

Note that a third-party library or DLL can install a console control handler for your application. If it does, this handler overrides the default handler, and can cause the application to exit when the user logs off.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms685049(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms683242(v=vs.85).aspx

SetConsoleCtrlHandler的更多相关文章

  1. 截获控制台程序关闭事件(SetConsoleCtrlHandler)

    最近控制台程序中需要捕获控制台关闭事件,在用户关闭的时候进行某些操作,找了一大圈发现了一个方法,通过调用WIN32 API SetConsoleCtrlHandler方法来实现,具体代码如下: usi ...

  2. SetConsoleCtrlHandler 处理控制台消息

    转载自csdn:http://blog.csdn.net/zhongguoren666/article/details/8770615   SetConsoleCtrlHandler 处理控制台消息 ...

  3. SetConsoleCtrlHandler演示

    #include "stdafx.h"#include <Windows.h> static BOOL WINAPI Handler(DWORD cntrlEvent) ...

  4. SetConsoleCtrlHandler() -- 设置控制台信号处理函数

    http://www.groad.net/bbs/thread-8253-1-1.html 当你在命令行里工作时,经常会输入 Ctrl-C 这个组合键以停止程序的运行.SetConsoleCtrlHa ...

  5. 再谈Delphi关机消息拦截 -- 之控制台程序 SetConsoleCtrlHandler(控制台使用回调函数拦截,比较有意思)

    这里补充一下第一篇文章中提到的拦截关机消息 Delphi消息拦截:http://blog.csdn.net/cwpoint/archive/2011/04/05/6302314.aspx 下面我再介绍 ...

  6. 物联网框架ServerSuperIO(SSIO)更新、以及增加宿主程序和配置工具,详细介绍

    一.更新内容 1.修改*Server类,以及承继关系.2.增加IRunDevice的IServerProvider接口继承.3.修复增加COM设备驱动可能造成的异常.4.修复网络发送数据可能引发的异常 ...

  7. [No000090]C#捕获控制台(console)关闭事件及响应cmd快捷键

    捕获控制台(console)关闭事件: 1.Ctrl+C信号: 2.Ctrl+Break信号: 3.用户系统关闭Console时: 4.用户退出系统时: 5.系统将要关闭时: using System ...

  8. GCHandler的使用

    众所周知,我们在使用c#托管代码时,内存地址和GC回收那不是我们关心的,CLR已经给我们暗箱操作. 但是如果我们在c#中调用了一个非托管代码,比如vc的DLL,而且他有个回调函数,需要引用c#中的某个 ...

  9. C#封装好的Win32API

    Kernel.cs using System; using System.Runtime.InteropServices; using System.Text; using HANDLE = Syst ...

随机推荐

  1. 洛谷 P2814 家谱

    题目背景 现代的人对于本家族血统越来越感兴趣. 题目描述 给出充足的父子关系,请你编写程序找到某个人的最早的祖先. 输入输出格式 输入格式: 输入由多行组成,首先是一系列有关父子关系的描述,其中每一组 ...

  2. UOJ #207. 共价大爷游长沙

    #207. 共价大爷游长沙 链接:http://uoj.ac/problem/207 题意:给一棵树,要求支持加边.删边.询问一条边是否被所有路径覆盖.同时路径端点集合有加入与删除操作. 想法: 考虑 ...

  3. java 网络流 TCP/UDP

    一.ServerSocket java.lang.Object |-java.net.ServerSocket 有子类SSLServerSocket. 此类实现服务器套接字.服务器套接字等待请求通过网 ...

  4. RPC电源监控总结

    首先说一下监控机箱的监控原理. 设备的信息传输是通过tcp或者udp传输十六进制的数然后进行解析,传输数据. 如图: 设备反馈信息也是返回来的十六机制,然后按照对应的位置进将数据解析成二进制,用二进制 ...

  5. jemter实战

    业务流梳理,需要实现的内容,含jdbc和正则表达式,和取值 1. 登录用户 2. 获取一个未分配的订单,判断是否是未分配 3. 获取配货单号 4. 分配配货单 5. 查询已分配的配货单 6. 查询包裹 ...

  6. 广搜破解密码(HDU1195)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1195 解题报告: #include<iostream> #include<cstdi ...

  7. ios RSA 验签加密解密

    关于公钥和私钥的生成,网上有很多本地生产的方法,我遇到的问题是,按照网上生产的方式生成7个文件,本地使用没有问题,但是和后台交互就不行了. 发现生成公钥和私钥的没有那么麻烦,使用在线生产工具就能使用, ...

  8. sql学习之创建表空间创建用户并授权

    --创建表空间语法:create tablespace [name]create tablespace hclTest--设置参数datafile 'F:/orcale/hclTest'--设置表空间 ...

  9. MySQL与SQLServer的区别(一千条语句)

    ER图.分页.差异.Java连接MySQL SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset LIMIT 子句可以被用于强制 ...

  10. javaweb基础(35)_jdbc处理oracl大数据

    一.Oracle中大数据处理 在Oracle中,LOB(Large Object,大型对象)类型的字段现在用得越来越多了.因为这种类型的字段,容量大(最多能容纳4GB的数据),且一个表中可以有多个这种 ...