473,411 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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 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
Nov 13 '05 #1
12 13698
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

--
Er*********@sun.com
Nov 13 '05 #2

Eric Sosman <Er*********@sun.com> wrote in message
news:3F***************@sun.com...
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

--
Er*********@sun.com


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

Wurm
Nov 13 '05 #3
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!

Nov 13 '05 #4
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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #5
CBFalconer <cb********@yahoo.com> 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) ks*@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"
Nov 13 '05 #6
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 : bi****@eton.powernet.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
Nov 13 '05 #7
In <3F***************@sun.com> Eric Sosman <Er*********@sun.com> 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: Da*****@ifh.de
Nov 13 '05 #8
In <bm**********@hercules.btinternet.com> Richard Heathfield <do******@address.co.uk.invalid> 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: Da*****@ifh.de
Nov 13 '05 #9
Dan Pop wrote:
Eric Sosman <Er*********@sun.com> 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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #10
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 : bi****@eton.powernet.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
Nov 13 '05 #11
In <3F**************@yahoo.com> CBFalconer <cb********@yahoo.com> writes:
Dan Pop wrote:
Eric Sosman <Er*********@sun.com> 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: Da*****@ifh.de
Nov 13 '05 #12
Thanks all, the signal() calls did the trick!. Ill definetly read up on that
FAQ
"Wurm" <a@b.c> wrote in message news:bl**********@dns3.cae.ca...
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

Nov 13 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Wugi | last post by:
I'm trying to find an equivalent of key-event trapping which is easy in QBasic. Several of my programs build up a geometric figure with two parameter line families, eg with two nested for..next...
8
by: Pete | last post by:
I'm trying to improve my code so that when I open a recordset object, I can absolutely guarantee it is closed and is set = Nothing. I have read some old threads and they all say to use the...
3
by: Paul | last post by:
I have an Access 2000 database with a form that is giving me some major headaches. When you open the form, it displays all records and allows editing, but has AllowAdditions set to False so that...
3
by: Nathan Bloomfield | last post by:
Hi there, I am having difficulty with a piece of code which would work wonders for my application if only the error trapping worked properly. Basically, it works as follows: - adds records...
3
by: windandwaves | last post by:
Hi Gurus Does anyone know how I set the error trapping to option 2 in visual basic. I know that you can go to tools, options and then choose on unhandled errors only, but is there a VB command...
13
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
1
by: Chirag Malvi | last post by:
hello all, I am developing the web application using ASP.net and VS.2003 IDE. here is the situation which i want to implement. 1) User is browsing some webform. I want to trap this event....
3
by: =B= | last post by:
Hi all, I was wondering if anyone has had any luck with trapping the <BODY> onUnload() event in ASP.NET? The thing is, I'm writing code for an Intranet site. The code makes a call to a...
2
by: Captain Nemo | last post by:
I'm still using Office 2000 myself, but some of my clients have Office 2003. I've recently added a piece of code to create an instance of Word, open a document, fill in the blanks and become...
9
by: 47computers | last post by:
Pretty new to PHP, I recently started learning about error trapping. As of right now, I include the following into a page in my website: -------BEGIN PASTE-------- error_reporting(E_ERROR |...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.