[python-win32] Re: Help: using msvcrt for file locking

Sheila King sheila@thinkspot.net
Sun, 26 Aug 2001 20:23:33 -0700

On Mon, 27 Aug 2001 12:11:10 +1000, "Mark Hammond"
<MarkH@ActiveState.com>  wrote about RE: [python-win32] Re: Help: using
msvcrt for file locking:

:Hrm - that is strange.  A quick check over the C++ extension code certainly
:implies it should work.
:> win32file.LockFile(fd, win32con.LOCKFILE_EXCLUSIVE_LOCK, 0, 0xffff0000,0)
:The LockFile documentation states that the first integer "Specifies the
:low-order word of the starting byte offset in the file where the lock should
:begin." - ie, it is *not* the flag that LockFileEx supports.  Set this to

Heh. Yah, I noticed that, but, like I said, I'm not really sure what
"low word order" is, and since you didn't mention to change that part in
the previous message, I shrugged and figured I'd leave it as is.

:The documentation for UnlockFile states that the unlocked region must be
:_exactly_ as specified in the LockFile call - hence your error when
:unlocking - the start offset is not identical.

Cool. This code ran without error:

filename = r'e:\apache\cgi-bin\pylinks\datalog.txt'

import win32con
import win32file
import pywintypes
from time import strftime, localtime, time

f = open(filename, 'a+')
fd = win32file._get_osfhandle(f.fileno())
win32file.LockFile(fd, 0, 0, 0xffff0000,0)
timestamp = strftime("%m/%d/%Y %H:%M:%S\n", localtime(time()))
win32file.UnlockFile(fd, 0, 0, 0xffff0000,0)

I'm not 100% sure what I did. It seems to me, that if Win9x/Me don't
take flags, then I'm simply getting an exclusive lock. There are no
shared locks for Win9x/Me, right?

It seems to me that I will probably be able to accomplish the same thing
with the msvcrt module. I finally realized, that I can have blocking or
non-blocking, but there is only exclusive locks and no shared locks.

:I would suggest buying a copy of the MSDN "library" on CD - I don't think it
:is too expensive, and is a very useful resource, including lots of sample
:code.  I have not purchased a Win32 API book in over 5 years (and even the
:one I got then was pretty useles :)

Thanks for the tip. I will look for that CD. Does it have stuff on it
that is not available on the msdn.microsoft.com website? I know that MS
books are not so hot. My husband bought one that was supposed to be the
official support for the Win95 OS, many years back, and many an
expletive I've heard about the lack of any useful information in that

Sheila King