Platform SDK: Files and I/O

LockFileEx

The LockFileEx function locks a region in an open file for shared or exclusive access. Locking a region prevents other processes from accessing the region.

BOOL LockFileEx(
  HANDLE hFile,                   // handle to file
  DWORD dwFlags,                  // lock options
  DWORD dwReserved,               // reserved
  DWORD nNumberOfBytesToLockLow,  // low-order word of length 
  DWORD nNumberOfBytesToLockHigh, // high-order word of length
  LPOVERLAPPED lpOverlapped       // starting offset
);

Parameters

hFile
[in] Handle to an open handle to a file that is to have a range of bytes locked for shared or exclusive access. The handle must have been created with either GENERIC_READ or GENERIC_WRITE access to the file.
dwFlags
[in] Specifies flags that modify the behavior of this function. This parameter may be one or more of the following values.
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.

dwReserved
Reserved parameter; must be set to zero.
nNumberOfBytesToLockLow
[in] Specifies the low-order 32 bits of the length of the byte range to lock.
nNumberOfBytesToLockHigh
[in] Specifies the high-order 32 bits of the length of the byte range to lock.
lpOverlapped
[in] Pointer to an OVERLAPPED structure that the function uses with the locking request. This structure, which is required, contains the file offset of the beginning of the lock range. You must initialize the hEvent member to a valid handle or zero.

Return Values

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.

Remarks

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.

The LockFileEx function operates asynchronously if the file handle was opened for asynchronous I/O, unless the LOCKFILE_FAIL_IMMEDIATELY flag is specified. If an exclusive lock is requested for a range of a file that already has a shared or exclusive lock, the function returns the error ERROR_IO_PENDING. The system will signal the event specified in the OVERLAPPED structure after the lock is granted. To determine when the lock has been granted, use the GetOverlappedResult function or one of the wait functions.

If the file handle was not opened for asynchronous I/O and the lock is not available, this call waits until the lock is granted or an error occurs, unless the LOCKFILE_FAIL_IMMEDIATELY flag is specified.

Locks may not overlap an existing locked region of the file.

Requirements

  Windows NT/2000: Requires Windows NT 3.1 or later.
  Windows 95/98: Unsupported.
  Header: Declared in Winbase.h; include Windows.h.
  Library: Use Kernel32.lib.

See Also

File I/O Overview, File I/O Functions, CreateFile, LockFile, OVERLAPPED, UnlockFile, UnlockFileEx