HRESULT _Must_inspect_result_ _Success_(SUCCEEDED(return)) UnLockFileRegions(
_In_ PFILE_INFO pFileInfo,
_In_ PLOCK_FILE_INFO pLockInfo
) ;
The UnLockFileRegions function releases file byte-range locks previously acquired via
LockFileRegions, attempting to unlock all specified regions even if individual unlock
operations fail. It implements best-effort semantics, ensuring reliable cleanup and per-region diagnostics.
�It implements best-effort semantics, ensuring that callers can reliably unlock multiple regions��
UnLockFileRegions complements LockFileRegions by providing symmetrical unlock
functionality with defensive error handling. Rather than aborting on the first failed unlock, the function
continues attempting all unlocks, recording success or failure for each region.
�This approach is critical for reliable resource management��
The function uses synchronous UnlockFile, ensuring deterministic unlock behavior.
Pointer to a valid FILE_INFO structure containing the file handle. Validated via
IsValidFileInfo. A NULL or invalid pointer causes immediate
ERROR_INVALID_PARAMETER.
�The file handle (hFile) is extracted from this structure and used for all UnlockFile calls.�
Structure defining the regions to unlock. Contains:
nLockRegions � number of regionsdwOffsetLow / dwOffsetHigh � 64-bit offsetdwLockLow / dwLockHigh � 64-bit sizebLocked � TRUE if region is currently lockedbUnlockResult � TRUE/FALSE per-region unlock result
A NULL pointer causes immediate ERROR_INVALID_PARAMETER.
Before unlocking, the function validates:
pLockInfo is non-NULLpFileInfo is validnLockRegions = MAX_LOCK_REGIONSTwo state variables track errors:
S_OK)�This becomes the function�s return value, ensuring the caller learns about at least one failure��
The function iterates over all regions:
bLocked == FALSE, skip region.UnlockFile with region�s offset and size.GetLastError()hrFirstbAnyFailed = TRUEbUnlockResult = FALSEbLocked = FALSEbUnlockResult = TRUE
After all regions are processed, the function returns:
bAnyFailed ? hrFirst : S_OK
�If all unlocks succeed, S_OK is returned��
bUnlockResult records success/failure.bLocked remains TRUE for failed unlocks.�This asymmetry is intentional� unlocking failures do not require rollback.�
After a call to LockFileRegions(pFileInfo, pLockInfo) that succeeded, a caller would later invoke UnLockFileRegions(pFileInfo, pLockInfo) using the same structures to release the locks. If all unlocks succeed, S_OK is returned and all regions' bUnlockResult fields are TRUE. If some unlocks fail (e.g., due to concurrent access or file handle corruption), the function returns the first error code, but bUnlockResult fields indicate which specific regions failed, allowing the caller to log detailed diagnostic information or attempt targeted cleanup. For example:
HRESULT hr = UnLockFileRegions(pFileInfo, pLockInfo);
if (FAILED(hr)) {
for (int i = 0; i < pLockInfo->nLockRegions; i++) {
if (!pLockInfo->LockRegions[i].bUnlockResult) {
// Region i failed to unlock
// Log or handle this specific failure
}
}
}