Re: Using SetConsoleCtrlHandler



This seems a bit fishy and not-recommendable to me... Get/SetThreadContext when you are in a system call will not have the expected result, some of the volatile registers (which might be parameter registers for fastcall, or even more problematic on x64) will be overwritten on return from the system call. And setting the context will not take effect until the system call returns anyway. There is also the problem that you don't have a way to wake a thread that was sleeping.

Furthermore, if you happen to catch a thread "at a bad time", such as when it owns an important lock (or even worse, is in the middle of acquiring an important lock, like the critical section for the process heap, such that you break recursive acquisition of it), you're likely to break the process (deadlock).

--
Ken Johnson (Skywing)
Windows SDK MVP
http://www.nynaeve.net
"Tony Proctor" <tony_proctor@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:eFnX5$BqHHA.1148@xxxxxxxxxxxxxxxxxxxxxxx
Windows is not very good at handling this sort of asynchronous interrupt on
a single thread Emmanuel (i.e. similar to UNIX signals, or even VMS ASTs)

The question has been asked before:
http://groups.google.ie/group/microsoft.public.win32.programmer.kernel/browse_frm/thread/608ad10204f76515/1e175f06dca6106f?hl=en#1e175f06dca6106f

I've even found myself in the same boat in trying to port a language, and
its framework, to the Windows O/S. In the end, I suspended the thread, read
its context, redirected it to a point that would generate the required
exception, and then released it. Surprisingly, it worked OK in practice
(although not on Alpha AXP H/W) but there were a few issues with win32 api
calls that had to be addressed (mentioned in that old thread)

Tony Proctor

"Emmanuel Stapf [ES]" <manus@xxxxxxxxxxxxxxxxx> wrote in message
news:uQhAUi9pHHA.1144@xxxxxxxxxxxxxxxxxxxxxxx
Hi,

I've a console single threaded application and I'm trying to catch a
Ctrl+C. No
matter if I use SetConsoleCtrlHandler or a signal handler, my code to
handle
this gets called in another thread. Is there a way to have the handler
called
from the main thread?

In the code below, simply comment the call to `signal' or to
`SetConsoleCtrlHandler' to observe the similar behavior. On Unix, using
`signal', it is called from the same thread.

Thanks for any highlight,
Manu

PS: this is shown by the code:

#include <windows.h>
#include <stdio.h>
#include <signal.h>

BOOL CtrlHandler( DWORD fdwCtrlType )
{
switch( fdwCtrlType ) {
case CTRL_C_EVENT:
printf( "Ctrl-C event\n\n" );
return TRUE;
default:
return FALSE;
}
}

void handler (int sig) {
printf ("From Signal\n");
signal (SIGINT, handler);
}

void main( void )
{
signal (SIGINT, handler);
//SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, TRUE );

printf("Use Ctrl+C to see what is going on.\n" );
while( 1 ){ }
}



.



Relevant Pages

  • [PATCH 9/11] UML - Implement soft interrupts
    ... This patch implements soft interrupts. ... later on, this pending signals flag is checked, and the IRQ handlers ... The enable_signals function has to loop because the IRQ handler ... void set_sigstack ...
    (Linux-Kernel)
  • Re: Using SetConsoleCtrlHandler
    ... POSIX signals to POSIX processes". ... void handler { ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Signal delivery order
    ... and the handler will be invoked when we return from ... Now, since there are no more pending signals, we return to the user ... int *page_address; ... void *eip ...
    (Linux-Kernel)
  • Re: Signal Handler and "-ansi -pedantic -Wall"
    ... The installed handler seems to be gone after the first run. ... void Handler{ ... I use global variables to record receipt of trapped signals. ... sigUsr1(int sig) ...
    (comp.unix.programmer)
  • Re: Using SetConsoleCtrlHandler
    ... the old thread says - it relied on correct exception handling elsewhere to ... a single thread Emmanuel (i.e. similar to UNIX signals, ... void handler { ... signal (SIGINT, handler); ...
    (microsoft.public.win32.programmer.kernel)

Loading