4 out of 14 rated this helpful - Rate this topic

LockFile function

Applies to: desktop apps only

Locks the specified file for exclusive access by the calling process.

To specify additional options, for example creating a shared lock or for block-on-fail operation, use the LockFileEx function.

Syntax

BOOL WINAPI LockFile(
  __in  HANDLE hFile,
  __in  DWORD dwFileOffsetLow,
  __in  DWORD dwFileOffsetHigh,
  __in  DWORD nNumberOfBytesToLockLow,
  __in  DWORD nNumberOfBytesToLockHigh
);

Parameters

hFile [in]

A handle to the file. The file handle must have been created with the GENERIC_READ or GENERIC_WRITE access right. For more information, see File Security and Access Rights.

dwFileOffsetLow [in]

The low-order 32 bits of the starting byte offset in the file where the lock should begin.

dwFileOffsetHigh [in]

The high-order 32 bits of the starting byte offset in the file where the lock should begin.

nNumberOfBytesToLockLow [in]

The low-order 32 bits of the length of the byte range to be locked.

nNumberOfBytesToLockHigh [in]

The high-order 32 bits of the length of the byte range to be locked.

Return value

If the function succeeds, the return value is nonzero (TRUE).

If the function fails, the return value is zero (FALSE). To get extended error information, call GetLastError.

Remarks

If the call to LockFile completes synchronously, a completion entry may not be queued when a completion port is associated with the file handle.

The UnlockFile function unlocks a file region locked by LockFile.

Locking a region of a file gives the threads of the locking process 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 region of a file does not prevent reading from a mapped file view.

You can lock bytes that are beyond the end of the current file. This is useful to coordinate adding records to the end of a file.

Exclusive locks cannot overlap an existing locked region of a file. For more information, see LockFileEx.

If LockFile cannot lock a region of a file, it returns zero immediately. It does not block. To issue a file lock request that will block until the lock is acquired, use LockFileEx without the LOCKFILE_FAIL_IMMEDIATELY flag.

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 operating system. However, the time it takes for the operating system 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 operating system has not yet unlocked them.

Examples

For an example, see Appending One File to Another File.

Requirements

Minimum supported client

Windows XP

Minimum supported server

Windows Server 2003

Header

FileAPI.h (include Windows.h);
WinBase.h on Windows Server 2008 R2, Windows 7, Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

See also

CreateFile
File Management Functions
LockFileEx
UnlockFile

 

 

Send comments about this topic to Microsoft

Build date: 4/17/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
.Net Version
see also: $0FileStream.Lock(long position, long length)$0 $0http://msdn.microsoft.com/en-us/library/system.io.filestream.lock.aspx$0 $0$0 $0 $0FileStream.Unlock(long position, long length)$0 $0http://msdn.microsoft.com/en-us/library/system.io.filestream.unlock.aspx$0 $0$0 $0
You don't need pinvoke or DLLimport
In managed code, you can just call the FileStream.Lock() method.
http://msdn.microsoft.com/en-us/library/system.io.filestream.lock.aspx
C# syntax
[DllImport("kernel32.dll", SetLastError=true)]
internal static extern bool LockFile(IntPtr handle, int offsetLow, int offsetHigh, int countLow, int countHigh);
vb.net syntax
<DllImport("kernel32.dll", SetLastError:=True)> _
Public Shared Function LockFile(ByVal handle As IntPtr, ByVal offsetLow As Integer, ByVal offsetHigh As Integer, ByVal countLow As Integer, ByVal countHigh As Integer) As Boolean
End Function