一段超简单但费解的代码,摘自msdn!!

winkiky 2003-12-04 04:44:53
BOOL CtrlHandler(DWORD fdwCtrlType)
{
switch (fdwCtrlType)
{
// Handle the CTRL+C signal.

case CTRL_C_EVENT:

Beep(1000, 1000);
return TRUE;

// CTRL+CLOSE: confirm that the user wants to exit.

case CTRL_CLOSE_EVENT:

return TRUE;

// Pass other signals to the next handler.

case CTRL_BREAK_EVENT:

case CTRL_LOGOFF_EVENT:

case CTRL_SHUTDOWN_EVENT:

default:

return FALSE;
}
}

void main(void)
{
BOOL fSuccess;

fSuccess = SetConsoleCtrlHandler(
(PHANDLER_ROUTINE) CtrlHandler, // handler function
TRUE); // add to list
if (! fSuccess)
MyErrorExit("Could not set control handler");
}
这个在控制台应该能够监测到ctrl+c和关闭消息,但是我这里怎么都失效了,系统根本没有调用回调函数!!
这是为何??盼高手明示,在线等待,马上揭帖!!!
...全文
35 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
beyondtkl 2003-12-04
  • 打赏
  • 举报
回复
學習..
ZhouBoTong 2003-12-04
  • 打赏
  • 举报
回复
gz
leon_z 2003-12-04
  • 打赏
  • 举报
回复
bool CtrlHandler(DWORD fdwCtrlType)
{
switch (fdwCtrlType)
{
// Handle the CTRL+C signal.

case CTRL_C_EVENT:

Beep(1000, 1000);
return TRUE;

// CTRL+CLOSE: confirm that the user wants to exit.

case CTRL_CLOSE_EVENT:

return TRUE;

// Pass other signals to the next handler.

case CTRL_BREAK_EVENT:

case CTRL_LOGOFF_EVENT:

case CTRL_SHUTDOWN_EVENT:

default:

return FALSE;
}
}

void main(void)
{
BOOL fSuccess;

fSuccess = SetConsoleCtrlHandler(
(PHANDLER_ROUTINE) CtrlHandler, // handler function
TRUE); // add to list

if (! fSuccess)
cout << "Could not set control handler" << endl;

cout << " Press Ctrl + C " << endl;
Sleep(10000);
char input;
cout << " Press Q or q " << endl;
for(;;)
{
cin >> input ;
if(input == 'Q' || input == 'q')
break;
}
cout << "Exit" << endl;
}
这样你就可以在按下Ctrl + C时听到BB声了
winkiky 2003-12-04
  • 打赏
  • 举报
回复
但是按照msdn的说法,这个回调函数应该类似钩子的那种,消息首先经过这个回调函数的,如果返回true,就不继续给下一个处理函数,否则才传递给缺省系统处理的。
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
leon_z 2003-12-04
  • 打赏
  • 举报
回复
你的程序一运行就执行完了,没有阻断和监控的话,对于基于事件编程,是没法实现的

你按下Ctrl + c时,程序都已经退出了

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧