LockFile

  BOOL LockFile(hFile, dwFileOffsetLow, dwFileOffsetHigh, cbLockLow, cbLockHigh)    
  HANDLE hFile; /* handle of file to lock */
  DWORD dwFileOffsetLow; /* low 32 bits of lock region offset */
  DWORD dwFileOffsetHigh; /* high 32 bits of lock region offset */
  DWORD cbLockLow; /* low 32 bits of length to lock */
  DWORD cbLockHigh; /* high 32 bits of length to lock */

The LockFile function locks a region in an open file. Locking a region prevents other processes from accessing the region.

Parameters

hFile

Identifies the file with a region to be locked. The file handle must have been created with GENERIC_READ and/or GENERIC_WRITE access to the file.

dwFileOffsetLow

Specifies the low 32 bits of the starting byte offset in the file where the lock should begin.

dwFileOffsetHigh

Specifies the high 32 bits of the starting byte offset in the file where the lock should begin.

cbLockLow

Specifies the low 32 bits of the length of the byte range to be locked.

cbLockHigh

Specifies the high 32 bits of the length of the byte range to be locked.

Return Value

The return value is TRUE if the function was successful, or FALSE if an error occurred. Use the GetLastError function to obtain extended error information.

Comments

Locking a region of a file gives the locking process exclusive access to the specified region of the file. File locks are not inherited by processes created by the locking process.

Locking a portion of a file 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.

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

The UnlockFile function unlocks a region of a file locked by LockFile.

For MS-DOS-based systems running SHARE.EXE, the lock semantics work as described above. Without SHARE.EXE, all attempts to lock or unlock a file will fail.

See Also

UnlockFile, CreateFile