Wednesday 16 November 2011

How can I prevent the user from breaking my program with Ctrl-Break? in C programming

How can I prevent the user from breaking my program with Ctrl-Break?

MS-DOS, by default, enables the user of a program to stop its execution by pressing Ctrl-Break. This is, in
most cases, a useful feature. It enables the user to exit in places from which a program might not allow exit, or from a program that has ceased to execute properly.

In some cases, however, this action might prove to be very dangerous. Some programs might carry out “secure” actions that, if broken, would give the user access to a private area. Furthermore, if the program is halted while updating a data file on disk, it might destroy the data file, perhaps destroying valuable data.

For these reasons, it might be useful to disable the Break key in some programs. A word of warning: delay placing this code in your program until you are 100 percent sure it will work! Otherwise, if your code

malfunctions and your program gets stuck, you might be forced to reboot the computer, perhaps destroying updates to the program.

Now I’ll show you how to disable the Break key. This is something of a special operation. It can’t be done on some machines, some do not have a Break key, and so on. There is therefore no special command in the C language for turning off the Break key. Furthermore, there is not even a standard way to do this on MSDOSmachines. On most machines, you must issue a special machine-language command. Here is a subroutine for turning off the Break key under MS-DOS:

#include <dos.h>
void StopBreak()
{
union REGS in , out;
in.x.ax = 0x3301;
in.x.dx = 0;
int86( 0x21 , &in , &out );
}

This subroutine creates a set of registers, setting the ax register to hexadecimal 3301, and the dx register to 0. It then calls interrupt hexadecimal 21 with these registers. This calls MS-DOS and informs it that it no longer wants programs to be stopped by the Break key.

Here’s a program to test this function:
#include <stdio.h>
#include <dos.h>
void StopBreak()
{
union REGS in , out;
in.x.ax = 0x3301;
in.x.dx = 0;
int86( 0x21 , &in , &out );
}
int main()
{
int a;
long b;
StopBreak();
for( a = 0 ; a < 100 ; ++ a )
{
StopBreak();
printf( “Line %d.\n” , a );
for( b = 0 ; b < 500000L ; ++ b );
}
return 0;
}

Cross Reference:

None.

No comments:

Post a Comment