次の方法で共有


LockFileEx (Compact 2013)

3/26/2014

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

Syntax

BOOL LockFileEx(
  HANDLE hFile, 
  DWORD dwFlags,
  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 possible values.

    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 Value

Nonzero indicates success. Zero indicates failure. For extended error information, call GetLastError.

Remarks

Locking a region of a file acquires 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, explicitly unlock all locked files when a process ends. Otherwise, access to these files can be denied if the OS has not yet unlocked them.

Requirements

Header

winbase.h

Library

coredll.lib

See Also

Reference

File I/O Functions
UnlockFileEx
OVERLAPPED