Overview
The LockFile function locks a region of a file, blocking the calling process until the operating system grants access to the region.
The LockFileEx function provides more control over the locking operation.
Tip: For robust file locking, especially in concurrent scenarios, consider using LockFileEx with appropriate flags.
Syntax
BOOL LockFile(
HANDLE hFile,
DWORD dwFileOffsetLow,
DWORD dwFileOffsetHigh,
DWORD nNumberOfBytesToLockLow,
DWORD nNumberOfBytesToLockHigh
);
This function is defined in fileapi.h.
Return Value
TRUE (BOOL): If the function succeeds, the return value is nonzero. Otherwise, the return value is zero. To get extended error information, call GetLastError.
A region can be locked by multiple processes, but the region must be locked by the same process before it can be unlocked.
A region is automatically unlocked when the handle to the file is closed.