Trapping Ctrl-C, how?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wurm

    Trapping Ctrl-C, how?

    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 SetWindowsHookE x 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


  • Eric Sosman

    #2
    Re: Trapping Ctrl-C, how?

    Wurm wrote:[color=blue]
    >
    > Hi all,
    >
    > Got a little problem [...][/color]

    .... which happens to be Question 19.38 in the comp.lang.c
    Frequently Asked Questions (FAQ) list



    --
    Eric.Sosman@sun .com

    Comment

    • Wurm

      #3
      Re: Trapping Ctrl-C, how?


      Eric Sosman <Eric.Sosman@su n.com> wrote in message
      news:3F83107C.D 1834264@sun.com ...[color=blue]
      > Wurm wrote:[color=green]
      > >
      > > Hi all,
      > >
      > > Got a little problem [...][/color]
      >
      > ... 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
      >
      > --
      > Eric.Sosman@sun .com[/color]

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

      Wurm


      Comment

      • E. Robert Tisdale

        #4
        Re: Trapping Ctrl-C, how?

        Eric Sosman wrote:
        [color=blue]
        > Wurm wrote:
        >[color=green]
        >>Got a little problem [...][/color]
        >
        > ... 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[/color]

        Excellent answer!

        Comment

        • CBFalconer

          #5
          Re: Trapping Ctrl-C, how?

          Wurm wrote:[color=blue]
          >
          > 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).[/color]

          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 (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
          Available for consulting/temporary embedded and systems.
          <http://cbfalconer.home .att.net> USE worldnet address!


          Comment

          • Keith Thompson

            #6
            Re: Trapping Ctrl-C, how?

            CBFalconer <cbfalconer@yah oo.com> writes:
            [...][color=blue]
            > 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[/color]
            [...]

            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_Keit h) kst@cts.com <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
            Schroedinger does Shakespeare: "To be *and* not to be"

            Comment

            • Richard Heathfield

              #7
              Re: Trapping Ctrl-C, how?

              CBFalconer wrote:
              [color=blue]
              > Most of that is OT on c.l.c. Maybe you want to find another way
              > of signalling the program to exit.
              >[/color]
              <snip>[color=blue]
              >
              > [#2] The atexit function registers the function pointed to
              > by func, to be called without arguments at normal program
              > termination.[/color]

              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 : binary@eton.pow ernet.co.uk
              "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

              Comment

              • Dan Pop

                #8
                Re: Trapping Ctrl-C, how?

                In <3F83107C.D1834 264@sun.com> Eric Sosman <Eric.Sosman@su n.com> writes:
                [color=blue]
                >Wurm wrote:[color=green]
                >>
                >> Hi all,
                >>
                >> Got a little problem [...][/color]
                >
                >... 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[/color]

                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@ifh.de

                Comment

                • Dan Pop

                  #9
                  Re: Trapping Ctrl-C, how?

                  In <bm01b6$6l2$2@h ercules.btinter net.com> Richard Heathfield <dontmail@addre ss.co.uk.invali d> writes:
                  [color=blue]
                  >CBFalconer wrote:
                  >[color=green]
                  >> Most of that is OT on c.l.c. Maybe you want to find another way
                  >> of signalling the program to exit.
                  >>[/color]
                  ><snip>[color=green]
                  >>
                  >> [#2] The atexit function registers the function pointed to
                  >> by func, to be called without arguments at normal program
                  >> termination.[/color]
                  >
                  >Does a SIGTERM signal count as "normal termination"?[/color]

                  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.
                  [color=blue]
                  >Perhaps the guy just needs to read[1] up on signal().[/color]

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

                  Dan
                  --
                  Dan Pop
                  DESY Zeuthen, RZ group
                  Email: Dan.Pop@ifh.de

                  Comment

                  • CBFalconer

                    #10
                    Re: Trapping Ctrl-C, how?

                    Dan Pop wrote:[color=blue]
                    > Eric Sosman <Eric.Sosman@su n.com> writes:[color=green]
                    > >Wurm wrote:[color=darkred]
                    > >>
                    > >> Got a little problem [...][/color]
                    > >
                    > > ... 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[/color]
                    >
                    > 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...[/color]

                    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 (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                    Available for consulting/temporary embedded and systems.
                    <http://cbfalconer.home .att.net> USE worldnet address!


                    Comment

                    • Richard Heathfield

                      #11
                      Re: Trapping Ctrl-C, how?

                      Dan Pop wrote:[color=blue]
                      > Richard Heathfield writes:[color=green]
                      >>CBFalconer wrote:
                      >>[/color][/color]
                      <snip>[color=blue][color=green][color=darkred]
                      >>> [#2] The atexit function registers the function pointed to
                      >>> by func, to be called without arguments at normal program
                      >>> termination.[/color]
                      >>
                      >>Does a SIGTERM signal count as "normal termination"?[/color]
                      >
                      > How can you send a SIGTERM signal to a Windows program? CTRL-C is
                      > supposed to send a SIGINT.[/color]

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

                      --
                      Richard Heathfield : binary@eton.pow ernet.co.uk
                      "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

                      Comment

                      • Dan Pop

                        #12
                        Re: Trapping Ctrl-C, how?

                        In <3F843F0B.FDBD0 F7@yahoo.com> CBFalconer <cbfalconer@yah oo.com> writes:
                        [color=blue]
                        >Dan Pop wrote:[color=green]
                        >> Eric Sosman <Eric.Sosman@su n.com> writes:[color=darkred]
                        >> >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[/color]
                        >>
                        >> 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...[/color]
                        >
                        >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.[/color]

                        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@ifh.de

                        Comment

                        • Wurm

                          #13
                          Re: Trapping Ctrl-C, how?

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


                          "Wurm" <a@b.c> wrote in message news:blv2ih$ko7 $1@dns3.cae.ca. ..[color=blue]
                          > 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[/color]
                          problem.[color=blue]
                          > 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[/color]
                          to[color=blue]
                          > my lib, if we do a Ctrl+C instead of the app dissapearing it hangs and[/color]
                          times[color=blue]
                          > 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[/color]
                          own[color=blue]
                          > '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?.[/color]
                          Bear[color=blue]
                          > in mind, I have no control over the creation of the window (I found a[/color]
                          group[color=blue]
                          > 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).[/color]
                          Ive[color=blue]
                          > tried the SetWindowsHookE x family of functions but for one reason or[/color]
                          another[color=blue]
                          > 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
                          >
                          >[/color]


                          Comment

                          Working...