Board index » delphi » Record Locking & Unlocking under Win95

Record Locking & Unlocking under Win95

I am trying to create a multiuser environment by locking and unlocking
records.
However, I am having difficulty in locking record numbers 48 and above in
my
typed random access file. The last error returned is always
87 (Parameter Invalid). Record numbers 0 to 47 lock without a problem.
My record size is 1416.

Please, could someone tell me what I am doing wrong ?

If possible please mail me direct at:

infoafr...@dial.pipex.co.za

//I have declared these variables global
var
  TestTypedFile_Handle: word;
  error_no: longint;

//Click this button to open the file in sharing mode
procedure TForm1.ButtonOpenTypedShareClick(Sender: TObject);
var
  fn_pchar: PChar;
  A: array[0..127] of char; //Assume maximum 128 character path name
  size_in_bytes: dword;

begin
  //Open typed file in SHARE mode
  new(fn_pchar);
  TestTypedFile_Handle := CreateFile(StrPCopy(A,TestTyped_fn),
                                     GENERIC_READ OR GENERIC_WRITE,
                                     FILE_SHARE_READ OR FILE_SHARE_WRITE,
                                     nil,  //No security attributes
                                     OPEN_EXISTING,
                                     FILE_ATTRIBUTE_NORMAL OR
                                     FILE_FLAG_RANDOM_ACCESS,
                                     0);
  dispose(fn_pchar);
  error_no := GetLastError;
end;

//Click this button to lock record number 48
procedure TForm1.ButtonLockClick(Sender: TObject);
var
  locked: boolean;

begin
  locked := LockTypedRec(48);
end;

//Lock specified record number in TestTypedFile with handle:
TestTypedFile_handle
function TForm1.LockTypedRec(rec: dword): boolean;
var
  locked: boolean;
  RecSize: dword;
  RecOffset: dword;

begin
  LockTypedRec := FALSE;
  //Get size of record to be locked
  RecSize := SizeOf(Client_RecType);
  //Get offset of record to be locked
  RecOffset := (rec-1)*RecSize;
  //Lock record
  locked := LockFile(TestTypedFile_Handle,
                     LoWord(RecOffset), HiWord(RecOffset),
                     LoWord(RecSize), HiWord(RecSize));
  if NOT locked then begin
     error_no := GetLastError;
     ShowMessage(IntToStr(error_no));
     //error_no is always 87 (Parameter invalid) if I attempt to lock
record
     //number 48 and up. Record size is 1416.
  end;
  LockTypedRec := locked;
end;

 

Re:Record Locking & Unlocking under Win95


In article <01bbca5a$b6473d10$574116c4@Donovan>,
infoafr...@pop.solo.pipex.co.za says...

Quote
> I am trying to create a multiuser environment by locking and unlocking
> records. However, I am having difficulty in locking record numbers 48
> and above in my
> typed random access file. The last error returned is always
> 87 (Parameter Invalid). Record numbers 0 to 47 lock without a problem.
> My record size is 1416.

I note two problems:

Quote
> function TForm1.LockTypedRec(rec: dword): boolean;
[snip]
>  RecOffset := (rec-1)*RecSize;

They both relate to this line.  You mention numbering your records from 0
- This gives a negative offset.  

Next, (47-1)*1416=65136 < 65535 < 66552=(48-1)*1416.  Looks like a 64K
boundary problem.  I don't have documentation for LockFile(), so I can't
help further.

Other Threads