home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / top2src.zip / TOPLINK.ZIP / FILELOCK.PAS < prev    next >
Pascal/Delphi Source File  |  1996-04-15  |  1KB  |  44 lines

  1. {
  2.  >MM> Is there a way to LOCK specific records or areas in a binary file so
  3.  >MM> that one program can access the 1st byte of file and another program
  4.  >MM> access the 2nd byte of the program at the same time?
  5.  
  6.  TC> Here's something from my own tool
  7.  TC> box of tricks:
  8. }
  9. function FLock(Lock:byte; Handle: Word; Pos,Len: LongInt): Word; Assembler;
  10.   ASM
  11.       mov   AL,Lock   { subfunction 0: lock region   }
  12.                       { subfunction 1: unlock region }
  13.       mov   AH,$5C    { DOS function $5C: FLOCK    }
  14.       mov   BX,Handle { put FileHandle in BX       }
  15.       les   DX,Pos
  16.       mov   CX,ES     { CX:DX begin position       }
  17.       les   DI,Len
  18.       mov   SI,ES     { SI:DI length lockarea      }
  19.       int   $21       { Call DOS ...               }
  20.       jb    @End      { if error then return AX    }
  21.       xor   AX,AX     { else return 0              }
  22.   @End:
  23. end {FLock};
  24.  
  25. Comes in handy when descending TDosStream.
  26.  
  27. TLockStream = object(TDosStream)
  28.                 procedure write(var buf;count:word); virtual;
  29.               end;
  30. Procedure TLockStream.write(var buf;count:word);
  31.    var isLocked : integer;
  32.        curpos   : longint;   
  33.    Begin
  34.      curpos := getpos;
  35.      isLocked := Flock(0,handle,curpos,count);
  36.      if   isLocked < 2
  37.      then begin
  38.             inherited write(buf,count);
  39.             if   isLocked = 0
  40.             then Flock(1,handle,curpos,count);
  41.           end
  42.      else status := isLocked;
  43.   End;
  44.