Share via


LockFileEx (Windows CE 5.0)

Send Feedback

This function locks the specified file for exclusive access by the calling process.

BOOL LockFileEx(HANDLE hFile, DWORDdwFlags,DWORD dwReserved,DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh,LPOVERLAPPED lpOverlapped);

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.
  • dwFlags
    [in] Flag value. The following table shows the possible values for dwFlags.
    Value Description
    LOCKFILE_EXCLUSIVE_LOCK The function requests an exclusive lock. If this flag is not specified, the function requests a shared lock.
    LOCKFILE_FAIL_IMMEDIATELY The function returns immediately if it is unable to acquire the requested lock. If this flag is not specified, the function waits.
  • dwReserved
    Reserved. Set to zero.
  • nNumberOfBytesToLockLow
    [in] Low-order 32 bits of the length of the byte range to lock.
  • nNumberOfBytesToLockHigh
    [in] High-order 32 bits of the length of the byte range to lock.
  • lpOverlapped
    [in] Pointer to an OVERLAPPED structure that is used with the lock request. This structure contains the file offset of the beginning of the lock range.

Return Values

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. For extended error information, call GetLastError.

Remarks

Locking a region of a file is used to acquire shared or exclusive access to the specified region using this file handle. If the file handle is inherited by a process created by the locking process, the child process is not granted access to the locked region. If the locking process opens the file a second time, it cannot access the specified region through this second handle until it unlocks the region.

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.

Locking a region of a file does not prevent reading from a mapped file view.

Exclusive locks cannot overlap an existing locked region of a file. Shared locks can overlap a locked region provided locks held on that region are only shared locks.

If a process terminates with a portion of a file locked or closes a file that has outstanding locks, the locks are unlocked by the OS. However, the time it takes for the OS to unlock these locks depends upon available system resources. Therefore, it is recommended that your process explicitly unlock all files it has locked when it terminates. If this is not done, access to these files may be denied if the OS has not yet unlocked them.

Requirements

OS Versions: Windows CE 5.0 and later.
Header: Winbase.h.
Link Library: Coredll.lib.

See Also

File I/O Functions | GetLastError | UnlockFileEx | OVERLAPPED

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.