UnlockFileEx function (fileapi.h)

Unlocks a region in the specified file. This function can operate either synchronously or asynchronously.


BOOL UnlockFileEx(
  [in]      HANDLE       hFile,
            DWORD        dwReserved,
  [in]      DWORD        nNumberOfBytesToUnlockLow,
  [in]      DWORD        nNumberOfBytesToUnlockHigh,
  [in, out] LPOVERLAPPED lpOverlapped


[in] hFile

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


Reserved parameter; must be zero.

[in] nNumberOfBytesToUnlockLow

The low-order part of the length of the byte range to unlock.

[in] nNumberOfBytesToUnlockHigh

The high-order part of the length of the byte range to unlock.

[in, out] lpOverlapped

A pointer to an OVERLAPPED structure that the function uses with the unlocking request. This structure contains the file offset of the beginning of the unlock range. You must initialize the hEvent member to a valid handle or zero. For more information, see Synchronous and Asynchronous I/O.

Return value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero or NULL. To get extended error information, call GetLastError.


Unlocking a region of a file releases a previously acquired lock on the file. The region to unlock must correspond exactly to an existing locked region. Two adjacent regions of a file cannot be locked separately and then unlocked using a single region that spans both locked regions.

Locks are released before the CloseHandle function is finished processing.

In Windows 8 and Windows Server 2012, this function is supported by the following technologies.

Technology Supported
Server Message Block (SMB) 3.0 protocol Yes
SMB 3.0 Transparent Failover (TFO) Yes
SMB 3.0 with Scale-out File Shares (SO) Yes
Cluster Shared Volume File System (CsvFS) Yes
Resilient File System (ReFS) Yes


Requirement Value
Minimum supported client Windows XP [desktop apps | UWP apps]
Minimum supported server Windows Server 2003 [desktop apps | UWP apps]
Target Platform Windows
Header fileapi.h (include Windows.h)
Library Kernel32.lib
DLL Kernel32.dll

See also

Locking and Unlocking Byte Ranges in Files


File Management Functions




Synchronous and Asynchronous I/O