Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to prevent user from stopping program w/ Ctrl-C or Ctrl-Break

Status
Not open for further replies.

therick

Programmer
Mar 20, 2001
34
US
I need to figure out a way to prevent a user from stopping my program before it finishes execution. In other words, I need to recognize a KeyPress event and handle it during runtime so that the executable will not stop. Any suggestions?
 
Can't you just caputre any keypress events, determine if the'yre a break sequence, and if they are just ignore them? I don't know if you can control what's passed to the OS or not.

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Yeah I think that is how it would be done but I have been unable to figure this out thus far. I have done something similar w/ VB but my C++ experience isn't as strong. Any sample code or pointers would be much appreciated.

Thanks
 
Hi,

If you are doing this under Windows, the full-proof way is to get the Windows Hook which will allow you to capture all Windows messages including Keyboard messages. You can 'handle' the message before it can be used for
anything else.

In DOS(or console program in Windows) however you may need to get hold of the keyboard interrupt, write kind of a TSR so that the OS doesn't catch the Ctrl-Break and then write your modification for the routine and finally call the default(old) routine.

U'll also require to get scan codes for Ctrl+Break(Ctrl=29) Break I don't know,just like asci character code of Ctrl+C = 3

Also, some keys, such as function keys, cursor keys, and ALT+KEY combinations, have no ASCII code. So,when a key is pressed, a microprocessor within the keyboard generates an “extended scan code” of two bytes. The first (low-order) byte contains the ASCII code, if any. The second (high-order) byte has the scan code—a unique code generated by the keyboard when a key is either pressed or released. Extended scan codes are more extensive to std ascii codes, so these can be used by programs to identify whether a particular key supports ascii code or not.

If you can elaborate more on the problem I could go towards a better solution.
 
Hi
If u r using SDK platform then search for the following in MSDN Console Control handler,HandlerRoutine and Registering a Control Handler Function
I guess this will help ur case definately.

Hemant.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top