The LockFileEx function locks a byte range within an open file for shared or exclusive access.
BOOL LockFileEx(
HANDLE hFile, // handle of file to lock
DWORD dwFlags, // functional behavior modification flags
DWORD dwReserved, // reserved, must be set to zero
DWORD nNumberOfBytesToLockLow,
// low-order 32 bits of length to lock
DWORD nNumberOfBytesToLockHigh,
// high-order 32 bits of length to lock
LPOVERLAPPED lpOverlapped
// addr. of structure with lock region start
// offset
);
Value | Meaning |
---|---|
LOCKFILE_FAIL_IMMEDIATELY | If this value is specified, the function returns immediately if it is unable to acquire the requested lock. Otherwise, it waits. |
LOCKFILE_EXCLUSIVE_LOCK | If this value is specified, the function requests an exclusive lock. Otherwise, it requests a shared lock. |
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero or NULL. To get extended error information, call GetLastError.
Locking a region of a file is used to acquire shared or exclusive access to the specified region of the file. File locks are not inherited by a new process during process creation.
Locking a portion of a file for exclusive access denies all other processes both read and write access to the specified region of the file. Locking a region that goes beyond the current end-of-file position is not an error.
Locking a portion of a file for shared access denies all processes write access to the specified region of the file, including the process that first locks the region. All processes can read the locked region.
If an exclusive lock is requested for a range of a file that already has a shared or exclusive lock, this call waits until the lock is granted, unless the LOCKFILE_FAIL_IMMEDIATELY flag is specified.
Locks may not overlap an existing locked region of the file.
Windows NT: Requires version 3.1 or later.
Windows: Unsupported.
Windows CE: Unsupported.
Header: Declared in winbase.h.
Import Library: Use kernel32.lib.
File I/O Overview, File Functions, CreateFile, LockFile, OVERLAPPED, UnlockFile, UnlockFileEx