ERROR_NOT_LOCKED - 158 (0x9E)
The segment is already unlocked.
Updated: Feb 21, 2026
Technical Background
The ERROR_NOT_LOCKED error code is a specific return value from the Windows API, indicating that an operation was attempted on an object that is already in an unlocked state. This error typically occurs during file or directory operations where the system expects the object to be locked.
Error Details
- Error Name: ERROR_NOT_LOCKED
- Numeric Code: 158 (0x9E)
- Hex Code: 0x9E
- Short Description: The segment is already unlocked.
This error suggests that the operation was attempted on a file or directory that is not locked, which may indicate an incorrect usage context or invalid parameter value. It can occur in various scenarios where locking mechanisms are expected to be in place before performing certain operations.
Common Causes
- Invalid Parameter Values: The operation was attempted with parameters that do not require the object to be locked.
- Incorrect Object Type: An attempt was made to lock a file or directory when it is already unlocked, or vice versa.
- Exceeding Limits: The system may have limitations on locking mechanisms that were not respected during the operation.
Real-World Context
This error can occur in various applications and scenarios where file operations are performed. For example, attempting to lock a file that is already unlocked, or performing an unlock operation on a file that has never been locked.
Is This Error Critical?
The criticality of this error depends on the context in which it occurs. If the application expects certain locking mechanisms to be in place and encounters this error, it may indicate a serious issue that needs to be addressed. However, if the operation was not dependent on the object being locked, the error might be benign.
How to Diagnose
- Review Operation Context: Ensure that the operation context is correct and that locking mechanisms are required for the intended action.
- Validate Parameters: Verify that the parameters passed to the API call are appropriate for the expected state of the object.
- Confirm Object Types: Check if the object type (file or directory) matches the expected behavior of the operation.
- Verify Input Data: Ensure that the input data is consistent with the locking requirements of the system.
- Check Limits or Constraints: Confirm that there are no system limits or constraints that could affect the locking state of the object.
How to Resolve
- Correct Parameter Usage: Adjust the parameters passed to the API call to ensure they reflect the correct state of the object.
- Adjust Operation Context: Modify the operation context if it is not appropriate for the current state of the object.
- Restore Data: If data corruption or incorrect states are suspected, restore the object to a known good state.
- Retry Operation with Valid Inputs: Attempt the operation again with valid inputs that respect the locking requirements.
Developer Notes
When working with file operations in Windows, it is crucial to understand the locking mechanisms and ensure that they are correctly applied based on the expected state of the object. This error can help identify issues related to incorrect usage or misconfiguration of these mechanisms.
Related Errors
- ERROR_LOCK_VIOLATION: Occurs when an attempt is made to lock a file that is already locked by another process.
- ERROR_SHARING_VIOLATION: Indicates that the operation could not be completed because the object was in use or locked by another process.
FAQ
Q: Can this error occur on directories as well?
A: Yes, ERROR_NOT_LOCKED can apply to both files and directories. The context of the operation determines whether the locking mechanism is required.
Q: What does it mean if an application encounters this error during a file unlock operation?
A: It means that the file was not locked in the first place, or the system expects it to be locked before performing the unlock operation.
Summary
ERROR_NOT_LOCKED is a specific return code indicating that an operation was attempted on an object that is already unlocked. This error can occur during various file and directory operations and requires careful review of the operation context and parameters to resolve. Understanding the locking mechanisms in Windows is crucial for avoiding this error.