WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: kdmoyers on April 01, 2024, 07:50:35 AM

Title: Read locked files?
Post by: kdmoyers on April 01, 2024, 07:50:35 AM
So I'm working on the old lock-out-simultaneous-users trick.
This works fine:
; LOCK HANDLING
 errormode(@off)
 lockhandle = FileOpen(lokf, "WRITE")
 errormode(@on)
 terminate(lockhandle == 0, mytitle:" Error", "Only one person may run %mytitle% at the same time.")


But this doesn't help the second user find the first user who is carelessly holding the lock.


So, I tried this:
; LOCK HANDLING
 intcontrol(40,1,0,0,0) ; allow read access to other processes
 errormode(@off)
 lockhandle = FileOpen(lokf, "WRITE")
 errormode(@on)
 if lockhandle == 0
 a = fileget(lokf) ; this fails with error "1800 Error opening file"
 terminate(1, mytitle:" Error", a:@crlf:"Only one person may run %mytitle% at the same time.")
 endif
 FileWrite(lockhandle, environment("USERNAME"):" ":environment("COMPUTERNAME"))

But it always fails for the second person on the fileget with error "1800 error opening file"

I was hoping the intcontrol 40 would permit the second person to read the file to see the
username and computer name written there by the first accessor. Where did I go wrong?
(I realize that I could use another file to hold the username, I was just hoping to use just 
the one lock file)
Title: Re: Read locked files?
Post by: td on April 01, 2024, 02:34:32 PM
A careful look at the documentation for IntControl 40 lists the functions affected by IntControl 40. FileGet is not one of them. Try IntControl 39 instead.
Title: Re: Read locked files?
Post by: spl on April 04, 2024, 05:11:14 AM
Quote from: kdmoyers on April 01, 2024, 07:50:35 AM(I realize that I could use another file to hold the username, I was just hoping to use just
the one lock file)

Yes, indeed - the old semaphore technique. Used that to control writing to Excel files by multiple users on network drives. Microsoft has an OpenFiles.exe utility to help with the "who" behind the file, but I assume you already know that.