Thread: How I can trap Ctrl+C and Ctrl+Break?

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    3

    Angry How I can trap Ctrl+C and Ctrl+Break?

    If we press the 'C' or 'Break' keys in combination with the 'Control' Key, it will terminate the execution of the program.

    How I can Stop this?

  2. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    in config.sys if you put BREAK=OFF it disables the ctrl+c command
    -

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    28

    io exceptions.

    i'm pretty sure that u can use IOexceptions to trap control characters.

    though that's C++ && java rather then C.

    look up try, catch, throw in a C++ book for more help.
    curiousity killed the cat, but it
    makes for one hell of a programmer.

    Alien.

  4. #4
    Unregistered
    Guest

    How I can trap Ctrl+C and Ctrl+Break?

    Alien.. you might want to look up signal handeling.

    I know that in linux a ^C will send a signal to your program.

    You can catch the signal and play with it how ever you want
    If its in windows im not sure.

    HTH

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,608
    CTRL-BRK is handled by int 23h


    Code:
    interrupt (*OldBreakHandler)(...)     //... for C++
    
    interrupt NewBreakHandler(...)
    {
      //Ctrl-brk has been pressed
      //If this is a NOP then do nothing here
     
    }
    
    void InstallHandler(void)
    {
      disable();      //CLI - no interrupts in this section
      OldBreakHandler=getvect(0x23);
      setvect(0x23,NewBreakHandler);
      enable();      //STI - turn interrupts back on
    }
    Upon exiting your code you should restore all patched vectors. There is no need to restore the int 23h vector according to the DOS tech refs. DOS is supposed to do this automatically.

    An alternate way to patch the vector table is this:

    segment=0x0000+(intnum*4)+2;
    offset=0x0000+(intnum*4);

    Turn this into a far function pointer using MK_FP and just call it like a normal function.

    I'm not sure if getvect() and setvect() use this or just call DOS to do it.

Popular pages Recent additions subscribe to a feed