Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Borland C++ 2.x ( with Turbo C ) - <b>harderr() establish a hardware error handler</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 harderr()               Establish a Hardware Error Handler

 #include   <dos.h>

 void       harderr(fptr);
 int        (*fptr)();                   Pointer to function

    harderr() enables a program to deal with hardware errors (interrupt
    0x24) by establishing an error handler pointed to by 'fptr'.
    harderr() actually establishes an MS-DOS interrupt handler, but does
    not call the handler itself.   When an interrupt 0x24 occurs, the
    handler function is called with the following arguments:

          handler(errval,ax,bp,si);
          int errval;         Error code set by MS-DOS in the DI register
          int ax;             Value set by MS-DOS in AX register
          int bp;             Value set by MS-DOS in BP register
          int si;             Value set by MS-DOS in SI register

    'ax' indicates a disk or other device error.  If 'ax' is non-
    negative, the problem is a disk error; if not, the problem is a
    device error.

                If a disk error is encountered, AND 'ax' with 0x00FF to
                get the number of the drive involved (1 = A, 2 = B, and
                so on).  'bp' and 'si' will point to the segment and
                offset, respectively, of the device driver header of the
                failing driver.  peek() and peekb() can be used to
                retrieve device information from the driver header, but
                neither poke() nor pokeb() can be used to alter the
                header.

                The error handler itself can either return to the calling
                program with hardrtn() or it can call hardresume() to
                return to MS-DOS.  In either case, the handler or the
                result code ('rescode') of hardresume() must provide for
                the traditional abort (2), retry (1) or ignore (0)
                option.  To abort the program, invoke MS-DOS interrupt
                0x23.

       Returns:     The handler must return:

                    0  =  ignore
                    1  =  retry
                    2  =  abort

         Notes:     The handler should avoid any 'bdos' calls other than
                    1 through 0x0C, to avoid corrupting the operating
                    system--especially all of the C++ standard I/O or UNIX-
                    emulation I/O calls.

   Portability:     MS-DOS only.

   -------------------------------- Example ---------------------------------

    The following statements replace the DOS "A)bort, R)etry, F)ail?"
    prompt with one that first checks to see if the error was caused by
    trying to write to a write-protected disk.


           #include <stdio.h>
           #include <dos.h>

           int handler(int errval, int ax, int bp, int si)
           {
               char drive;

               if (ax < 0) {                      /* device error */
                   bdosptr(0x9, "DOS device error$", 0);
                   hardretn(-1);                  /* return to program */
               }
               drive = 'A' + (ax & 0xFF);
               if (errval == 0) {       /* write protect error */
                   bdosptr(0x9, "Put a disk which is NOT
                            write protected in drive $", 0);
                   bdos(0x2, drive, 0);  /* write drive name */
                   bdosptr(0x9, ": <RETURN>$", 0);
                   bdos(0x8, 0, 0);       /* wait for a key to be pressed */
                   bdosptr(0x9, "\r\n$", 0);
                   hardresume(1);       /* tell DOS to RETRY */
               }
                  /* some other disk error */
               bdosptr(0x9, "Disk error on drive $", 0);
               bdos(0x2, drive, 0);
               return(2);                          /* ABORT program */
           }

           main()
           {
           FILE *f;

               harderr(handler);   /* attach error handler */

               printf("Hit a key to try to create file on drive A:\n");
               getch();
               if ((f = fopen("A:\NEWFILE.NEW", "w+")) == NULL)
                    printf("Can't open file!\n");
               printf("DONE\n");
           }


See Also: hardresume() hardretn() setjmp()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson