home *** CD-ROM | disk | FTP | other *** search
- (* WP -- A demonstration unit to disable trapping of write protect errors.
-
- This file includes a sample custom critical error handler for the INT24 unit
- for Turbo Pascal version 4.0. The INT24 unit allows installation of special
- handlers to be installed before the default handler is called and deal with
- special cases. In this case, we decided not to allow retries after attempting
- to write to a disk with a write-protect tab.
- A skeleton critical error handler must look like this:
-
- var SaveCriticalProc: pointer;
-
- {$F+}
- procedure CriticalErrorHandler(var Retry: boolean;
- ErrorCode: word;
- var DeviceName: string);
- begin
- if ErrorCode = { Whatever errors we are interested in }
- then
- { Handle the error }
- else
- { Pass on any unwanted errors to other critical error handlers }
- CriticalProc := SaveCriticalProc
- end;
- {$F-}
-
- begin
- SaveCriticalProc := CriticalProc; { This installs your handler }
- CriticalProc := @CriticalErrorHandler;
- { Rest of your unit or program }
- end.
-
- The compiler directives ensure that the handler routine is compiled as a FAR
- procedure and this is _absolutely_ necessary. Note that there is no error
- checking that the procedure header matches this example, but if it doesn't the
- program will die when a critical error does occur. The Retry flag is set true
- in your handler if you decide you want to retry the operation. To abort the
- operation, leave Retry false and simply end the procedure. To pass on control
- to the next error handler, leave Retry false and restore the value of
- CriticalProc. The ErrorCode parameter will be a equal to the value return by
- DOS for the critical error and ranges from 0 to 12 in the same order as those
- errors from Turbo 4 (i.e. 0 corresponds to 150 and is write-protect error).
- DeviceName is a string variable that will either contain the drive the error
- occured on (such as 'A:'), the character device the error occured on (such as
- 'PRN') or be empty ('') in the case of a bad FAT.
- In general the flow of logic in a critical error handler is to first decide
- if this is an error type the handler is interested in by checking ErrorCode.
- If not, then restore the address that was in CriticalProc before you installed
- this handler. This permits a series of independant critical error handlers to
- be installed at the same time. If your handler deals with an error, then
- leave CriticalProc alone and any other handlers will be disabled for this
- critical error. Assuming that this is an error you want to deal with, do
- whatever you want to do (display a message, correct the problem, or whatever)
- and either leave Retry alone (which will cause the function to fail and
- your code deals with the error) or set Retry to true and let DOS try the
- operation again. Note that you are very limited as to what you can do in a
- critical error handler. Generally, you may not use any DOS calls but rather
- must either interface with the hardware directly or use BIOS calls. The
- default handler uses BIOS calls and direct access to the display through the
- FastWr unit. This is a good choice.
- To install a critical error handler, save the current contents of the
- CriticalProc variable and then set CriticalProc to the address of your
- critical error handler. No facility for uninstalling critical error handlers
- is provided.
- Compile this file and compare its operation to the test in INT24.DOC. The
- only difference should be an immeadiate error result when writing to a disk
- with a write protect tab. *)
-
-
- program Test;
-
- uses Int24;
-
- (******************* This is the special handler *******************)
- var SaveCriticalProc: pointer;
-
- {$F+}
- procedure CriticalErrorHandler(var Retry: boolean;
- ErrorCode: word;
- var DeviceName: string);
- { Automatically abort on write-protect errors }
- begin
- if ErrorCode = 0 { Watch for write protect error }
- then
- { Do absolutly nothing to abort automatically }
- else
- CriticalProc := SaveCriticalProc { Pass error on to default handler }
- end;
- {$F-}
- (*******************)
-
- {$I-}
-
- var I: Integer;
-
- procedure FileTest;
- var F: file;
- begin
- writeln('Testing for critical errors by writing to drive A:');
- I := IOResult;
- assign(F,'A:FILE');
- I := IOResult;
- rewrite(F);
- I := IOResult;
- if I <> 0
- then
- writeLn('Create failure on A:FILE : IOResult=',I)
- else
- begin
- writeln('A:FILE created.');
- I := IOResult;
- close(F);
- I := IOResult
- end
- end;
-
- begin
- SaveCriticalProc := CriticalProc; { This installs the special handler }
- CriticalProc := @CriticalErrorHandler;
- FileTest
- end.