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) |