Author Topic: Ctrl-c exception handling works on Mac, not on PC  (Read 6856 times)

GreatBigBore

  • Guest
Ctrl-c exception handling works on Mac, not on PC
« on: September 21, 2013, 02:54:57 AM »
I've written a program for a school assignment that catches exceptions. Among the exceptions I'm catching is ctrl-c. This works great on my Mac, but not on the PCs at school. Specifically, on the PC, ctrl-c does not generate an exception. Worse, when ctrl-c is hit, the program doesn't halt immediately; it continues to run for just a bit longer. I know this I wrote the code to issue a message to the user if the computation was successful, and although I hit ctrl-c, I see the "success" message. Baffling. I searched the documentation and found only one reference to the issue. It says that if ctrl-c handling is enabled, you can get the ctrl-c exception. But I can't find anything anywhere about how to enable ctrl-c handling, or why it would be enabled by default on the Mac and disabled by default on the PC. Anyone have any suggestions?

Adamanteus

  • Member
  • **
  • Posts: 180
Re: Ctrl-c exception handling works on Mac, not on PC
« Reply #1 on: September 21, 2013, 03:14:01 AM »
SetConsoleCtrlHandler - sets handle, in it possible count, that all events are break program events, and raise signal, exception or set flag variable for check it later.

dedndave

  • Member
  • *****
  • Posts: 8734
  • Still using Abacus 2.0
    • DednDave
Re: Ctrl-c exception handling works on Mac, not on PC
« Reply #2 on: September 21, 2013, 04:07:40 AM »
to capture all types of Exceptions, you want SEH (Structured Exception Handling)
not a simple task for beginners, but.....

here is one by Alex...

http://masm32.com/board/index.php?topic=350.msg2275

there are a few different ones in this thread - a couple by Michael look promising
and - Andy posted a simple one by Jeremy that i translated to Masm syntax
it could use a little cleanup, as i don't think it uses the "official" structure and member names

http://masm32.com/board/index.php?topic=997.msg9093

as for a Ctrl-C handler - give me a few minutes, and i'll post a simple example.....

Adamanteus mentioned SetConsoleCtrlHandler
you may also want to look at SetConsoleMode

dedndave

  • Member
  • *****
  • Posts: 8734
  • Still using Abacus 2.0
    • DednDave
Re: Ctrl-c exception handling works on Mac, not on PC
« Reply #3 on: September 21, 2013, 04:16:15 AM »
Quote
CTRL+BREAK is always treated as a signal, but typical CTRL+C behavior can be changed in three ways that prevent the handler functions from being called:

    The SetConsoleMode function can disable the ENABLE_PROCESSED_INPUT mode for a console's input buffer, so CTRL+C is reported as keyboard input rather than as a signal.
    Calling SetConsoleCtrlHandler with the NULL and TRUE arguments causes the calling process to ignore CTRL+C signals. This attribute is inherited by child processes, but it can be enabled or disabled by any process without affecting existing processes.
    If a console process is being debugged and CTRL+C signals have not been disabled, the system generates a DBG_CONTROL_C exception. This exception is raised only for the benefit of the debugger, and an application should never use an exception handler to deal with it. If the debugger handles the exception, an application will not notice the CTRL+C, with one exception: alertable waits will terminate. If the debugger passes the exception on unhandled, CTRL+C is passed to the console process and treated as a signal, as previously discussed.

dedndave

  • Member
  • *****
  • Posts: 8734
  • Still using Abacus 2.0
    • DednDave
Re: Ctrl-c exception handling works on Mac, not on PC
« Reply #4 on: September 21, 2013, 04:53:15 AM »
give this a try...

i believe "processed input" is enabled, by default
otherwise, you may have to use SetConsoleMode to set it
for example, a keyboard input routine might disable it

i call a routine that uses msvcrt functions to read individual keystrokes   :P