This function locks the specified file for exclusive access by the calling process.
BOOL LockFileEx( HANDLE hFile, DWORD dwFlags, DWORD dwReserved, DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh, LPOVERLAPPED lpOverlapped );
[in] Handle to an open handle to a file that is to have a range of bytes locked for shared or exclusive access.
[in] Flag value. The following table shows possible values.
The function requests an exclusive lock. If this flag is not specified, the function requests a shared lock.
The function returns immediately if it is unable to acquire the requested lock. If this flag is not specified, the function waits.
Reserved. Set to zero.
[in] Low-order 32 bits of the length of the byte range to lock.
[in] High-order 32 bits of the length of the byte range to lock.
[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.
Nonzero indicates success. Zero indicates failure. For extended error information, call GetLastError.
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.