Welcome to Microsoft Q&A!
Thank you for taking the time to provide such detailed information and for clearly describing the constraints around preserving the profile and the existing evidence.
There is no documented API that allows a later process to reacquire or reconstruct the hProfile returned by an earlier successful LoadUserProfile call after that handle has been lost.
1. Documented API contract
LoadUserProfile requires a user token with TOKEN_QUERY, TOKEN_IMPERSONATE, and TOKEN_DUPLICATE access. The calling process must also have SE_RESTORE_NAME and SE_BACKUP_NAME privileges and, beginning with Windows XP SP2 and Windows Server 2003, must be an administrator or LocalSystem.
After a successful call, PROFILEINFO.hProfile contains a registry-key handle opened with KEY_ALL_ACCESS to the root of the user's registry hive. Microsoft specifically states that the caller should not close this handle directly. Instead, it should be passed to UnloadUserProfile, which closes it.
UnloadUserProfile requires:
- A token for the user with
TOKEN_IMPERSONATEandTOKEN_DUPLICATEaccess. - The
hProfilevalue from thePROFILEINFOstructure. - Administrative privileges.
- All handles opened by the application under the user's registry hive to be closed before the unload operation.
A successful call returns TRUE. Otherwise, GetLastError provides the extended error information.
Microsoft's published user-profile API reference documents LoadUserProfile and UnloadUserProfile, but it does not document an API for attaching a new caller to an existing outstanding load by SID, profile path, or hive name.
2. Why a new handle does not replace the original context
Obtaining another token for the same user does not by itself reconstruct the lost hProfile.
Likewise, opening HKEY_USERS\SID again gives the new process another registry-key handle. The documented UnloadUserProfile contract, however, identifies its hProfile parameter as the hProfile member of the PROFILEINFO structure associated with LoadUserProfile.
DuplicateHandle can duplicate a registry-key handle between processes, but it requires a valid source handle in the context of the source process and PROCESS_DUP_HANDLE access to the relevant process handles. Therefore, it can be useful as part of a deliberately designed handle-transfer mechanism while the source handle remains available, but it does not provide a documented way to recover a handle that has already been lost when its owning process terminated.
3. Does process termination automatically unload the profile?
I would not rely on process termination alone as evidence that the original LoadUserProfile operation has been balanced.
The LoadUserProfile documentation explicitly distinguishes closing a handle from unloading the profile: hProfile should be passed to UnloadUserProfile, rather than closed directly.
Event ID 1530 should also not be interpreted by itself as evidence that an orphaned LoadUserProfile operation was successfully recovered. Microsoft documents this event as occurring when Windows automatically closes registry handles left open by an application while Windows is trying to close the user profile. The behavior is by design, and the application leaving the handle open should be investigated.
4. Supported recovery position
If the original hProfile and appropriate user-token context still exist, the documented cleanup path is to:
- Close the registry handles opened by the application beneath the user's hive.
- Call UnloadUserProfile with the appropriate user token and the hProfile returned by LoadUserProfile.
- Verify that the call returns TRUE; if it fails, capture GetLastError.
In the situation described here, however, the process has already terminated and the original hProfile and retained token are unavailable. The published user-profile API documentation does not provide an in-place API for reconstructing that lost LoadUserProfile context.
Given your requirement to preserve the existing state and evidence, I would therefore leave the profile state unchanged rather than substitute a newly opened registry handle, perform another compensating LoadUserProfile/UnloadUserProfile sequence, or directly unload the hive.
RegUnLoadKey, for example, is documented as a registry operation that unloads a key previously loaded with RegLoadKey, It is separate from the documented LoadUserProfile/UnloadUserProfile lifecycle.
If an in-place resolution is required while restart/logoff and a new profile load are prohibited, the safest supported course is to preserve the current evidence and escalate the case for deeper Microsoft Support investigation rather than attempting to reconstruct or bypass undocumented User Profile Service state.
5. Verification and evidence collection
It is a recommendation starting profile load/unload troubleshooting with the Application log and then correlating relevant timestamps with theUser Profile Service > Operational log.
If those logs are insufficient, Microsoft also documents more detailed diagnostic and ETL tracing for User Profile Service.
For the documented UnloadUserProfile path, the primary programmatic success criterion is that UnloadUserProfile returns TRUE. Otherwise, GetLastError should be captured.
Win32_UserProfile.Loaded, the presence of the SID hive, and User Profile Service events can be retained as observational evidence, but they should not be treated as a replacement for the documented UnloadUserProfile return value when that API path is available.
6. Unsupported substitutions
For this scenario, I would not present any of the following as a documented recovery procedure:
- Passing a newly opened
HKEY_USERS\SIDhandle as though it were the lostPROFILEINFO.hProfile. - Treating a newly obtained token for the same SID as ownership of the previous load.
- Performing another
LoadUserProfile/UnloadUserProfilepair and assuming that it balances the earlier operation. - Using
RegUnLoadKeyas a substitute forUnloadUserProfile.RegUnLoadKeyis documented for registry hives loaded through RegLoadKey, not as the inverse of LoadUserProfile. - Changing ACLs, privileges, security policy, or profile data merely to force an unload.
Conclusion:
So, for the scenario described, there is no documented supported API for reacquiring the lost hProfile/load context after the process that called LoadUserProfile has terminated. The documented cleanup operation is UnloadUserProfile, using the appropriate user token and the hProfile associated with the corresponding LoadUserProfile operation.
Since those inputs are no longer available and preserving the profile and evidence is a requirement, I would avoid substituting registry handles or using direct hive operations. Preserve the current state, collect the relevant User Profile Service and Application events, and escalate for Microsoft Support investigation if an in-place recovery is required without an approved restart, logoff, or new profile load.
References: LoadUserProfileW function (userenv.h) - Win32 apps | Microsoft Learn
UnloadUserProfile function (userenv.h) - Win32 apps | Microsoft Learn
User Profiles Reference - Win32 apps | Microsoft Learn
Event ID 1530 Is Logged in the Application Log - Windows Server | Microsoft Learn
DuplicateHandle function (handleapi.h) - Win32 apps | Microsoft Learn
PROFILEINFOW (profinfo.h) - Win32 apps | Microsoft Learn
Win32_UserProfile class | Microsoft Learn
RegUnLoadKeyW function (winreg.h) - Win32 apps | Microsoft Learn
ProcessStartInfo.LoadUserProfile Property (System.Diagnostics) | Microsoft Learn
Troubleshoot user profiles with events - Windows Server | Microsoft Learn
User State Migration Tool (USMT) common issues - Windows Client | Microsoft Learn
If you find it useful, please click Accept Answer.
Thank you for choosing Microsoft Q&A.
Note: This is the ไทย (th-th) forum. I kindly recommend posting your question in ไทย, as this will make it easier for other forum members and subject matter experts to engage with your inquiry. If you prefer to post in English, you may also consider using the English forum, where a larger English -speaking audience can review and respond to your question. Thank you for your understanding.