Chinaunix首页 | 论坛 | 博客
  • 博客访问: 102344
  • 博文数量: 11
  • 博客积分: 1581
  • 博客等级: 上尉
  • 技术积分: 241
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-13 01:12
文章分类

全部博文(11)

文章存档

2012年(3)

2011年(3)

2010年(5)

我的朋友

分类: WINDOWS

2011-07-27 10:16:13

hide console:
#pragma comment(linker,"/subsystem:windows  /entry:wmainCRTStartup") 

console handler:
handling console application events is to setup an even trap, technically referred to as installing an event handler.SetConsoleCtrlHandler Win32 API:
BOOL SetConsoleCtrlHandler(
PHANDLER_ROUTINE HandlerRoutine, // handler function
BOOL Add // add or remove handler
);
The HandlerRoutine parameter is a pointer to a function.
BOOL WINAPI HandlerRoutine(
DWORD dwCtrlType // control signal type
);

The parameter dwCtrlType can take the following values:

CTRL_C_EVENT - occurs when the user presses CTRL+C, or when it is sent by the GenerateConsoleCtrlEvent API.
CTRL_BREAK_EVENT - occurs when the user presses CTRL+BREAK, or when it is sent by the GenerateConsoleCtrlEvent API.
CTRL_CLOSE_EVENT - occurs when attempt is made to close the console, when the system sends the close signal to all processes associated with a given console.
CTRL_LOGOFF_EVENT - occurs when the user is logging off. One cannot determine, however, which user is logging off.
CTRL_SHUTDOWN_EVENT - occurs when the system is being shutdown, and is typically sent to services.

BOOL WINAPI ConsoleHandler(DWORD CEvent)
{
switch(CEvent)
{
case CTRL_C_EVENT:
wprintf(L"CTRL_C_EVENT.\n");
break;
case CTRL_BREAK_EVENT:
wprintf(L"CTRL_BREAK_EVENT.\n");
break;
case CTRL_CLOSE_EVENT:
wprintf(L"CTRL_CLOSE_EVENT.\n");
break;
case CTRL_LOGOFF_EVENT:
wprintf(L"CTRL_LOGOFF_EVENT.\n");
break;
case CTRL_SHUTDOWN_EVENT:
wprintf(L"CTRL_SHUTDOWN_EVENT.\n");
break;

}
return TRUE;
}

use the SetConsoleCtrlHandler API:

if (SetConsoleCtrlHandler((PHANDLER_ROUTINE)ConsoleHandler,TRUE)==FALSE)
{
wprintf(L"Unable to install handler!\n");
return -1;
}

reference:


阅读(2399) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~