trapping control c and control break
trapping control c and control break
i'm writing a program that should not allow control-c or control-break but it might be handy to trap rather than suppress them. is there any way to do that without 'on error' ?
CTL-BEAK
There definitely is. I think using a BIOS INT (instead INKEY$) does thei'm writing a program that should not allow control-c or control-break but it might be handy to trap rather than suppress them. is there any way to do that without 'on error' ?
job correctly.
You should read up on the crt function 'signal' and associated topics.
I haven't used it before successfully, but this example seems to work OK here, but it may not be the correct way to use it, and here the sub foo seems to get called twice each time I press ctrl-c
If you read up on it further, you may be able to find out more about it, but here's something to get started with.
On Windows/DOS the function might need to be declared alias "_signal"
and may not work at all, I can't test that, it seems OK on linux
I haven't used it before successfully, but this example seems to work OK here, but it may not be the correct way to use it, and here the sub foo seems to get called twice each time I press ctrl-c
If you read up on it further, you may be able to find out more about it, but here's something to get started with.
On Windows/DOS the function might need to be declared alias "_signal"
and may not work at all, I can't test that, it seems OK on linux
Code: Select all
#define SIGINT 2
#define SIG_IGN cptr(sighandler_t, 1)
Type sighandler_t As Sub cdecl (ByVal As Integer)
Declare Function signal cdecl alias "signal" ( _
ByVal signum As Integer, _
ByVal handler As sighandler_t _
) As sighandler_t
Sub foo cdecl (ByVal sig As Integer)
Print "Signal " & sig & " trapped."
End Sub
' According to the docs I read, if the handler is set to ignore, you should
' respect that.
If signal(SIGINT, @foo) = SIG_IGN Then signal(SIGINT, SIG_IGN)
While Inkey <> "1"
Sleep 100
Wend
-
- Posts: 605
- Joined: Feb 18, 2006 13:30
- Location: Alexandria / Egypt
- Contact:
-
- Posts: 4
- Joined: Jun 06, 2012 19:46
Re: trapping control c and control break
While the above code works great compiled with -lang fb, I am having trouble getting it to work with -lang qb. I keep getting the error that the signal array has not been dimensioned. I am guessing it is because of the Type statement or the Declare statement (or the pair).
The original issue is that I am trying to disable these key combos (Ctrl-C and Ctrl-Break) from exiting my program. I modified the above code to also trap the SIGBREAK(21) signal, and then inside the foo function, added the "IF signal ..." lines again to set the trap again as the first time foo is called, the signal is then reset to the original handler. (ie: The first Ctrl-C is trapped and execution is sent to foo but the second Ctrl-C will exit the program. By setting the signals SIGINT and SIGBREAK back to point to the foo function within the function itself, the trap stays active.) Of course if all you want to do is ignore the Ctrl-C and Ctrl-Break combos, just use the following once at the beginning of the program and eliminate the foo function:
signal(SIGINT, SIG_IGN)
signal(SIGBREAK, SIG_IGN)
Now if the original code cannot be modified to compile using the -lang qb switch, can I place this code in a separate file and have only that file compiled using the metacommand '$lang: "fb"? Is it possible to compile some files using FB and other files using QB to create the program?
The original issue is that I am trying to disable these key combos (Ctrl-C and Ctrl-Break) from exiting my program. I modified the above code to also trap the SIGBREAK(21) signal, and then inside the foo function, added the "IF signal ..." lines again to set the trap again as the first time foo is called, the signal is then reset to the original handler. (ie: The first Ctrl-C is trapped and execution is sent to foo but the second Ctrl-C will exit the program. By setting the signals SIGINT and SIGBREAK back to point to the foo function within the function itself, the trap stays active.) Of course if all you want to do is ignore the Ctrl-C and Ctrl-Break combos, just use the following once at the beginning of the program and eliminate the foo function:
signal(SIGINT, SIG_IGN)
signal(SIGBREAK, SIG_IGN)
Now if the original code cannot be modified to compile using the -lang qb switch, can I place this code in a separate file and have only that file compiled using the metacommand '$lang: "fb"? Is it possible to compile some files using FB and other files using QB to create the program?
Re: trapping control c and control break
In the old days you could do this by hooking Interrupt 23h and returning from the handler with an IRET. DOS sets the handler when the program starts, and copies the address of the parent’s handler to the program’s PSP, so the parent’s handler can be restored automatically when the program terminates (so IOW there is no need to unhook your handler).
Edit: After about an hour of trying, I have not been able to make it work.
Edit: After about an hour of trying, I have not been able to make it work.
Re: trapping control c and control break
Hi, same issue here.DodgeRules wrote:While the above code works great compiled with -lang fb, I am having trouble getting it to work with -lang qb. I keep getting the error that the signal array has not been dimensioned. I am guessing it is because of the Type statement or the Declare statement (or the pair).
Of course if all you want to do is ignore the Ctrl-C and Ctrl-Break combos, just use the following once at the beginning of the program and eliminate the foo function:
signal(SIGINT, SIG_IGN)
signal(SIGBREAK, SIG_IGN)
I am on Linux and compiling using -lang qb
Trying to ignore Ctrl+C and Ctrl+Break, not trap them and run a subroutine. I just want the program to ignore them.