Trapping Ctrl-C, how?

Discussion in 'C Programming' started by Wurm, Oct 7, 2003.

  1. Wurm

    Wurm Guest

    Hi all,

    Got a little problem with a lib that I'm working on. First of all, the
    problem is that the lib that I have makes connections to DirectSound, and
    has a host of custom built sound related functions. Now, in the lib is a
    'destroy' function which when called will release all instances of
    DirectSound that I have created and if that is used, then great, no problem.
    The application that uses this lib is a console application running on
    WindowsNT 4.0 sp6, and before connecting to this lib, if we wanted to kill
    the app we simply hit Ctrl+C and it dies. But now that its been connected to
    my lib, if we do a Ctrl+C instead of the app dissapearing it hangs and times
    out which unfortunately is unnaceptable for our usage. So to solve this, I
    either have to find a way to trap when the user hits Ctrl+C and call my own
    'destroy' function before releasing the Ctrl+C back to the OS, or, have to
    find a way in DirectSound to configure it so that if the controlling app
    dies, the objects created are released automatically (this I've already
    looked for and cant find).

    So.... thanks for reading this far!, does anyone know how to do this?. Bear
    in mind, I have no control over the creation of the window (I found a group
    of functions which seem that they would do the job, WindowProc and friends
    but they need to be fully setup at the window creation which I cant do). Ive
    tried the SetWindowsHookEx family of functions but for one reason or another
    I cannot get any test hooks to trigger at all and I was told that they may
    not function properly with console applications.

    Any input at all would be greatly appreciated, post here or if you prefer
    mail me directly at: pforan AT cae DOT com

    TIA!

    Phil
    Wurm, Oct 7, 2003
    #1
    1. Advertising

  2. Wurm

    Eric Sosman Guest

    Eric Sosman, Oct 7, 2003
    #2
    1. Advertising

  3. Wurm

    Wurm Guest

    Eric Sosman <> wrote in message
    news:...
    > Wurm wrote:
    > >
    > > Hi all,
    > >
    > > Got a little problem [...]

    >
    > ... which happens to be Question 19.38 in the comp.lang.c
    > Frequently Asked Questions (FAQ) list
    >
    > http://www.eskimo.com/~scs/C-faq/top.html
    >
    > --
    >


    Well son-of-a......., thanks!. Ill definetly take some time and look through
    that FAQ, didnt even know it was there :)

    Wurm
    Wurm, Oct 7, 2003
    #3
  4. Eric Sosman wrote:

    > Wurm wrote:
    >
    >>Got a little problem [...]

    >
    > ... which happens to be Question 19.38 in the comp.lang.c
    > Frequently Asked Questions (FAQ) list
    >
    > http://www.eskimo.com/~scs/C-faq/top.html


    Excellent answer!
    E. Robert Tisdale, Oct 7, 2003
    #4
  5. Wurm

    CBFalconer Guest

    Wurm wrote:
    >
    > Got a little problem with a lib that I'm working on. First of all,
    > the problem is that the lib that I have makes connections to
    > DirectSound, and has a host of custom built sound related
    > functions. Now, in the lib is a 'destroy' function which when
    > called will release all instances of DirectSound that I have
    > created and if that is used, then great, no problem. The
    > application that uses this lib is a console application running
    > on WindowsNT 4.0 sp6, and before connecting to this lib, if we
    > wanted to kill the app we simply hit Ctrl+C and it dies. But
    > now that its been connected to my lib, if we do a Ctrl+C instead
    > of the app dissapearing it hangs and times out which
    > unfortunately is unnaceptable for our usage. So to solve this, I
    > either have to find a way to trap when the user hits Ctrl+C and
    > call my own 'destroy' function before releasing the Ctrl+C back
    > to the OS, or, have to find a way in DirectSound to configure it
    > so that if the controlling app dies, the objects created are
    > released automatically (this I've already looked for and cant
    > find).


    Most of that is OT on c.l.c. Maybe you want to find another way
    of signalling the program to exit.

    However if Ctrl-C causes the program to perform a normal exit
    (so-called), the following STANDARD function may help. From N869.

    7.20.4.2 The atexit function

    Synopsis

    [#1]
    #include <stdlib.h>
    int atexit(void (*func)(void));

    Description

    [#2] The atexit function registers the function pointed to
    by func, to be called without arguments at normal program
    termination.

    Environmental limits

    [#3] The implementation shall support the registration of at
    least 32 functions.

    Returns

    [#4] The atexit function returns zero if the registration
    succeeds, nonzero if it fails.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Oct 7, 2003
    #5
  6. CBFalconer <> writes:
    [...]
    > Most of that is OT on c.l.c. Maybe you want to find another way
    > of signalling the program to exit.
    >
    > However if Ctrl-C causes the program to perform a normal exit
    > (so-called), the following STANDARD function may help. From N869.
    >
    > 7.20.4.2 The atexit function

    [...]

    Ctrl-C typically causes a signal to be sent to the currently running
    program. That generally doesn't result in a normal exit, so it
    bypasses any atexit()-registered functions.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
    Keith Thompson, Oct 8, 2003
    #6
  7. CBFalconer wrote:

    > Most of that is OT on c.l.c. Maybe you want to find another way
    > of signalling the program to exit.
    >

    <snip>
    >
    > [#2] The atexit function registers the function pointed to
    > by func, to be called without arguments at normal program
    > termination.


    Does a SIGTERM signal count as "normal termination"?

    Perhaps the guy just needs to read[1] up on signal().


    [1] I first typed this as "reed". I think I must be getting fnetic in my old
    aj.

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Oct 8, 2003
    #7
  8. Wurm

    Dan Pop Guest

    In <> Eric Sosman <> writes:

    >Wurm wrote:
    >>
    >> Hi all,
    >>
    >> Got a little problem [...]

    >
    >... which happens to be Question 19.38 in the comp.lang.c
    >Frequently Asked Questions (FAQ) list
    >
    > http://www.eskimo.com/~scs/C-faq/top.html


    What the FAQ doesn't say is that the implementation of SIGINT in
    Windows is broken: the signal handler will be executed in a new thread,
    rather than having the program execution suspended until the signal
    handler returns. This has bitten more than one unsuspecting Windows
    programmer...

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Oct 8, 2003
    #8
  9. Wurm

    Dan Pop Guest

    In <bm01b6$6l2$> Richard Heathfield <> writes:

    >CBFalconer wrote:
    >
    >> Most of that is OT on c.l.c. Maybe you want to find another way
    >> of signalling the program to exit.
    >>

    ><snip>
    >>
    >> [#2] The atexit function registers the function pointed to
    >> by func, to be called without arguments at normal program
    >> termination.

    >
    >Does a SIGTERM signal count as "normal termination"?


    How can you send a SIGTERM signal to a Windows program? CTRL-C is
    supposed to send a SIGINT.

    Anyway, the simple delivery of any of them does not cause normal program
    termination: they're merely asking the program to terminate. It's up to
    the program to decide what to do.

    >Perhaps the guy just needs to read[1] up on signal().


    Preferably, from the Windows documentation, for a reason mentioned
    in another post.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Oct 8, 2003
    #9
  10. Wurm

    CBFalconer Guest

    Dan Pop wrote:
    > Eric Sosman <> writes:
    > >Wurm wrote:
    > >>
    > >> Got a little problem [...]

    > >
    > > ... which happens to be Question 19.38 in the comp.lang.c
    > > Frequently Asked Questions (FAQ) list
    > >
    > > http://www.eskimo.com/~scs/C-faq/top.html

    >
    > What the FAQ doesn't say is that the implementation of SIGINT in
    > Windows is broken: the signal handler will be executed in a new
    > thread, rather than having the program execution suspended until
    > the signal handler returns. This has bitten more than one
    > unsuspecting Windows programmer...


    Which, I assume, means that the handler must set a "global" flag,
    and that the program must check that at predetermined places to
    handle such a CTL-C operation.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Oct 8, 2003
    #10
  11. Dan Pop wrote:
    > Richard Heathfield writes:
    >>CBFalconer wrote:
    >>

    <snip>
    >>> [#2] The atexit function registers the function pointed to
    >>> by func, to be called without arguments at normal program
    >>> termination.

    >>
    >>Does a SIGTERM signal count as "normal termination"?

    >
    > How can you send a SIGTERM signal to a Windows program? CTRL-C is
    > supposed to send a SIGINT.


    Yes, you're right. Two likely candidates to choose from, so obviously I
    chose the wrong one.

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Oct 8, 2003
    #11
  12. Wurm

    Dan Pop Guest

    In <> CBFalconer <> writes:

    >Dan Pop wrote:
    >> Eric Sosman <> writes:
    >> >Wurm wrote:
    >> >>
    >> >> Got a little problem [...]
    >> >
    >> > ... which happens to be Question 19.38 in the comp.lang.c
    >> > Frequently Asked Questions (FAQ) list
    >> >
    >> > http://www.eskimo.com/~scs/C-faq/top.html

    >>
    >> What the FAQ doesn't say is that the implementation of SIGINT in
    >> Windows is broken: the signal handler will be executed in a new
    >> thread, rather than having the program execution suspended until
    >> the signal handler returns. This has bitten more than one
    >> unsuspecting Windows programmer...

    >
    >Which, I assume, means that the handler must set a "global" flag,
    >and that the program must check that at predetermined places to
    >handle such a CTL-C operation.


    A strategy that doesn't work very well if the program is stuck into an
    input call that never returns, for one reason or another...

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Oct 9, 2003
    #12
  13. Wurm

    Wurm Guest

    Thanks all, the signal() calls did the trick!. Ill definetly read up on that
    FAQ


    "Wurm" <> wrote in message news:blv2ih$ko7$...
    > Hi all,
    >
    > Got a little problem with a lib that I'm working on. First of all, the
    > problem is that the lib that I have makes connections to DirectSound, and
    > has a host of custom built sound related functions. Now, in the lib is a
    > 'destroy' function which when called will release all instances of
    > DirectSound that I have created and if that is used, then great, no

    problem.
    > The application that uses this lib is a console application running on
    > WindowsNT 4.0 sp6, and before connecting to this lib, if we wanted to kill
    > the app we simply hit Ctrl+C and it dies. But now that its been connected

    to
    > my lib, if we do a Ctrl+C instead of the app dissapearing it hangs and

    times
    > out which unfortunately is unnaceptable for our usage. So to solve this, I
    > either have to find a way to trap when the user hits Ctrl+C and call my

    own
    > 'destroy' function before releasing the Ctrl+C back to the OS, or, have to
    > find a way in DirectSound to configure it so that if the controlling app
    > dies, the objects created are released automatically (this I've already
    > looked for and cant find).
    >
    > So.... thanks for reading this far!, does anyone know how to do this?.

    Bear
    > in mind, I have no control over the creation of the window (I found a

    group
    > of functions which seem that they would do the job, WindowProc and friends
    > but they need to be fully setup at the window creation which I cant do).

    Ive
    > tried the SetWindowsHookEx family of functions but for one reason or

    another
    > I cannot get any test hooks to trigger at all and I was told that they may
    > not function properly with console applications.
    >
    > Any input at all would be greatly appreciated, post here or if you prefer
    > mail me directly at: pforan AT cae DOT com
    >
    > TIA!
    >
    > Phil
    >
    >
    Wurm, Oct 9, 2003
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Danny

    Implement Ctrl-C, Ctrl-V

    Danny, Aug 14, 2003, in forum: C++
    Replies:
    5
    Views:
    634
    CBFalconer
    Aug 15, 2003
  2. Danny

    Implement Ctrl-C, Ctrl-V

    Danny, Aug 14, 2003, in forum: C Programming
    Replies:
    5
    Views:
    648
    CBFalconer
    Aug 15, 2003
  3. Angus
    Replies:
    5
    Views:
    2,085
    Michael Rauscher
    Nov 18, 2006
  4. Replies:
    2
    Views:
    1,393
    Munna
    Jul 10, 2008
  5. Replies:
    0
    Views:
    547
Loading...

Share This Page