
Technical Information Database
TI170D.txt Network File Access
Category :General Programming
Platform :All
Product :Pascal All
Description:
FILE AND RECORD LOCKING IN TURBO PASCAL
Turbo Pascal does not have built-in support for file and record
locking. It does, however, have the ability to open files for
shared access. This is accomplished by setting the value of the
system variable FileMode. By default, FileMode has a value of 2
which causes files opened to have single-user, read/write access.
A shared, read/write mode can be had by setting FileMode to a
value of 66. Whereas, a shared, read only mode is represented by
the value 64.
Record and file locking is a bit more difficult. DOS version 3.0
and later provide record locking through the use of Interrupt
21H, Service 5CH. This service may be accessed through Turbo
Pascal's procedure MsDos defined in the Dos unit.
Here's a routine designed to lock and unlock portions of any
untyped or type file. Note, these routines may not be used with
text files.
procedure LockBlock(var F; Offset: Longint; Length: LongInt;
Lock: Boolean);
type
LoHiRec = record
LoWord, HiWord: Word;
end;
var
RecordSize: Word;
Handle: Integer;
File: FileRec absolute F;
R: Registers;
begin
Handle := File.Handle;
RecordSize := File.RecSize;
Offset := Offset * RecordSize;
R.CX := LoHiRec(Offset).LoWord;
R.DX := LoHiRec(Offset).HiWord;
R.SI := LoHiRec(Length).LoWord;
R.DI := LoHiRec(Length).HiWord;
R.AH := $5C;
if Lock then R.AL := 0 else R.AL := 1;
MsDos(R);
if (R.Flags and FCarry) <> 0 then
begin
RunError(R.AX);
end;
end;
The parameter 'offset' indicates what record is to mark the
beginning of the file lock. 'Length' determines how many records
will be locked from this point. The 'lock' parameter determines
whether the portion of the file is to be locked or unlocked.
To lock an entire file, pass 0 for offset and the results of the
FileSize function for the number of records to be locked.
This routine will function as expected when using SHARE and/or
MS-DOS compatible network software.
Reference:
7/16/98 4:33:38 PM
|