Microsoft KB Archive/297060

= How To Load and Unload a User Profile into the Registry with Visual Basic =

Article ID: 297060

Article Last Modified on 7/1/2004

-

APPLIES TO

 Microsoft Visual Basic 5.0 Enterprise Edition, when used with:  Microsoft Windows 2000 Standard Edition

 Microsoft Windows NT 4.0  Microsoft Visual Basic 6.0 Enterprise Edition, when used with:  Microsoft Windows 2000 Standard Edition

 Microsoft Windows NT 4.0</li></ul> </li> Microsoft Visual Basic 5.0 Professional Edition, when used with:  Microsoft Windows 2000 Standard Edition</li></ul>

 Microsoft Windows NT 4.0</li></ul> </li> Microsoft Visual Basic 6.0 Professional Edition, when used with:  Microsoft Windows 2000 Standard Edition</li></ul>

 Microsoft Windows NT 4.0</li></ul> </li> Microsoft Visual Basic 5.0 Learning Edition, when used with:  Microsoft Windows 2000 Standard Edition</li></ul>

 <li>Microsoft Windows NT 4.0</li></ul> </li> <li>Microsoft Visual Basic 6.0 Learning Edition, when used with: <ul> <li>Microsoft Windows 2000 Standard Edition</li></ul>

<ul> <li>Microsoft Windows NT 4.0</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q297060

<div class="notice_section">

IMPORTANT: This article contains information about modifying the registry. Before you modify the registry, make sure to back it up and make sure that you understand how to restore the registry if a problem occurs. For information about how to back up, restore, and edit the registry, click the following article number to view the article in the Microsoft Knowledge Base:

256986 Description of the Microsoft Windows Registry

<div class="summary_section">

SUMMARY
This article describes how to use the RegLoadKey registry function to load a user profile into the registry and, subsequently, how to use RegUnLoadKey to unload the user profile. Because RegLoadKey requires the SE_RESTORE_NAME privilege to be successful, this article also uses the OpenProcessToken, LookupPrivilegeValue, and AdjustTokenPrivileges functions.

<div class="moreinformation_section">

MORE INFORMATION
In part, the registry consists of files that store information about a user profile. When this file is loaded, it maps to the HKEY_USERS or HKEY_LOCAL_MACHINE key, whichever is specified in the call to RegLoadKey.

To retrieve user profile-specific information, you can load the NtUser.dat file that is located in the profile path of the user profile that you want to load. It may be necessary to load a hive (user profile) when you try to provide profile-specific data. For example, either the ImpersonateLoggedOnUser function or the CreateProcessAsUser function is generally used to run under a different security context and does not load the profile of that user.

The following steps illustrate how to load NtUser.dat and unload it when finished. These methods are not a threat to security because they only succeed if the calling process and the impersonated user have sufficient privileges.

Step-by-Step Example
WARNING: If you use Registry Editor incorrectly, you may cause serious problems that may require you to reinstall your operating system. Microsoft cannot guarantee that you can solve problems that result from using Registry Editor incorrectly. Use Registry Editor at your own risk.

<ol> <li>Create a new Standard EXE project in Visual Basic. Form1 is created by default.</li> <li>Add a TextBox control (Text1) and two CommandButton controls (Command1 and Command2) to Form1.</li> <li> Paste the following code into the General Declarations section of Form1: Option Explicit

Private Type LUID LowPart As Long HighPart As Long End Type

Private Type LUID_AND_ATTRIBUTES pLuid As LUID Attributes As Long End Type

Private Type TOKEN_PRIVILEGES PrivilegeCount As Long Privileges(1) As LUID_AND_ATTRIBUTES End Type

Private Const TOKEN_ADJUST_PRIVLEGES = &H20 Private Const TOKEN_QUERY = &H8 Private Const SE_PRIVILEGE_ENABLED = &H2 Private Const HKEY_USERS = &H80000003 Private Const SE_RESTORE_NAME = &quot;SeRestorePrivilege&quot; Private Const SE_BACKUP_NAME = &quot;SeBackupPrivilege&quot;

Private Declare Function GetCurrentProcess Lib &quot;kernel32&quot; As Long

Private Declare Function OpenProcessToken Lib &quot;advapi32.dll&quot; _ (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, _                            TokenHandle As Long) As Long Private Declare Function LookupPrivilegeValue Lib &quot;advapi32.dll&quot; Alias _ &quot;LookupPrivilegeValueA&quot; (ByVal lpSystemName As String, _ ByVal lpName As String, lpLuid As LUID) As Long

Private Declare Function AdjustTokenPrivileges Lib &quot;advapi32.dll&quot; _ (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, _ NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, _ ByVal PreviousState As Long, ByVal ReturnLength As Long) As Long

Private Declare Function RegLoadKey Lib &quot;advapi32.dll&quot; Alias &quot;RegLoadKeyA&quot; _ (ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpFile As String) _ As Long

Private Declare Function RegUnLoadKey Lib &quot;advapi32.dll&quot; Alias &quot;RegUnLoadKeyA&quot; _ (ByVal hKey As Long, ByVal lpSubKey As String) As Long

Private Retval As Long Private strKeyName As String Private MyToken As Long Private TP As TOKEN_PRIVILEGES Private RestoreLuid As LUID Private BackupLuid As LUID

Private Sub Form_Load strKeyName = &quot;keyLoaded&quot; ' Path to file on Windows NT: C:\WinNT\Profiles\<Profile Name>\NtUser.Dat ' Path to file on Windows 2000: C:\Documents and Settings\<Profile Name>\NtUser.Dat Text1.Text = &quot;<Path to File>&quot; Command2.Enabled = False Retval = OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVLEGES _      Or TOKEN_QUERY, MyToken) If Retval = 0 Then MsgBox &quot;OpenProcess: &quot; & Err.LastDllError Retval = LookupPrivilegeValue(vbNullString, SE_RESTORE_NAME, _      RestoreLuid) If Retval = 0 Then MsgBox &quot;LookupPrivileges: &quot; & Err.LastDllError Retval = LookupPrivilegeValue(vbNullString, SE_BACKUP_NAME, BackupLuid) If Retval = 0 Then MsgBox &quot;LookupPrivileges: &quot; & Retval TP.PrivilegeCount = 2 TP.Privileges(0).pLuid = RestoreLuid TP.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED TP.Privileges(1).pLuid = BackupLuid TP.Privileges(1).Attributes = SE_PRIVILEGE_ENABLED Retval = AdjustTokenPrivileges(MyToken, vbFalse, TP, Len(TP), 0&, 0&) If Retval = 0 Then MsgBox &quot;AdjustTokenPrivileges: &quot; & Err.LastDllError End Sub Private Sub Command1_Click Retval = RegLoadKey(HKEY_USERS, strKeyName, Text1.Text) If Retval <> 0 Then MsgBox &quot;RegLoadKey: &quot; & Retval Command2.Enabled = True End Sub

Private Sub Command2_Click Retval = RegUnLoadKey(HKEY_USERS, strKeyName) If Retval <> 0 Then MsgBox &quot;RegUnloadKey: &quot; & Retval End Sub

Private Sub Form_Unload(Cancel As Integer) Retval = AdjustTokenPrivileges(MyToken, vbTrue, TP, Len(TP), 0&, 0&) If Retval = 0 Then MsgBox &quot;AdjustTokenPrivileges: &quot; & Err.LastDllError End Sub </li> <li>Save the project, and then press the F5 key to run it.</li> <li>Type the path to a specific user profile .dat file, for example:

C:\WinNT\Profiles\Administrator\NtUser.dat

and then click Command1.</li> <li>Click Start, click Run, type regedit (on Windows NT) or regedt32 (on Windows 2000), and then click OK.</li> <li>Locate the HKEY_USERS subtree. Notice that this subtree includes the new key, KeyLoaded.</li> <li>In the Visual Basic project, click Command2 to remove this key from the registry.</li></ol>

<div class="references_section">