如何在console application 中获取按健(拒绝hook)(50分)

  • 主题发起人 主题发起人 白河愁
  • 开始时间 开始时间

白河愁

Unregistered / Unconfirmed
GUEST, unregistred user!
比如 ctrl+c,要求程序不被该键中断,谢谢。
 
参考一下:
/*********************************************************************
* FUNCTION: demoSetCtrlHandler(HANDLE hConOut) *
* *
* PURPOSE: demonstrate SetConsoleCtrlHandler by setting a ctrl+break *
* and ctrl+c handler. When the user hits either one of *
* these keys, a message is printed to the console *
* indicating the event. *
* *
* INPUT: the output handle to write to *
*********************************************************************/

void demoSetCtrlHandler(HANDLE hConOut)
{
BOOL bSuccess;

setConTitle(__FILE__);
hConsole = hConOut
/* set global console output handle for handler */
myPuts(hConOut, "Let's install a ctrl+c and ctrl+break handler for this/n"
"process. Hit ctrl+c and ctrl+break a few times to test/n"
"the handler. Hit enter to return...");
/* set handler for this process */
bSuccess = SetConsoleCtrlHandler(handler_routine, TRUE);
PERR(bSuccess, "SetConsoleCtrlHandler");
/* wait for user to hit enter */
while (myGetchar() != 0xd)
;
/* now let's generate some control events */
myPuts(hConOut, "Now we'll use GenerateConsoleCtrlEvent to generate a/n"
"ctrl+c and a ctrl+break event.../n");
bSuccess = GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
PERR(bSuccess, "GenerateConsoleCtrlEvent");
bSuccess = GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, 0);
PERR(bSuccess, "GenerateConsoleCtrlEvent");
Sleep(1000)
/* give ctrl handle time to output messages */
myPuts(hConOut, "/nNow choose 'Close' then 'Cancel' from the system/n"
"menu of this console and note that we receive a/n"
"CTRL_CLOSE_EVENT.../n");
myPuts(hConOut, "/nHit enter to continue...");
myGetchar();
/* remove our handler from the list of handlers */
bSuccess = SetConsoleCtrlHandler(handler_routine, FALSE);
PERR(bSuccess, "SetConsoleCtrlHandler");
return;
}
 
这个东西不全吧。myGetchar 是什么?
 
你的e-mail,我发给你。
 
Registering a Control Handler Function
This section shows 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, causing the system to display a dialog box that gives the user the choice of terminating the process and closing the console or allowing the process to continue execution. If the user chooses not to terminate the process, the system closes the console when the process finally terminates.

When a ctrl+break, CTRL_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.

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

}

 
还是C++ BUILDER的代码比较好看,谢谢各位。
 
后退
顶部