We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


How to restore a registry tree?

Tarod asked
Medium Priority
Last Modified: 2008-03-10
How can I make a restore of a registry tree?
I tried with RegSaveKey and  RegRestoreKey, but the second function seems not work. 8-(
Watch Question

If you used RegSaveKey to save a registry key, then you would need to use RegLoadKey to load it back in to the registry.


No, 'cause if I use RegLoadKey I can only use HKEY_USERS or HKEY_LOCAL_MACHINE and I need to use HKEY_CURRENT_USER in a deep subkey level. The sublevel that I need to use is "Software\Microsoft\Internet Mail and News" all the hive below that key. And the only way that seems to work is RegRestoreKey. This function seems to accept a open handle of a subkey. Thanks a lot.

As a roundabout way, you could use RegEnumKey to find all the keys and values contained in  the key you are working with, then write each subkey and value to a file using RegQueryValueEx.   Like I said, it's kind of roundabout, but it would get you there.

Tarod, are you trying to move a subkey and everything subordinate?  There's an easy way to do that.


dirtdart: I only want to make the roundabout work of RegEnumKey in the last of all the cases. If I finally use this method point go for you. Thanks.

mrmick: No, I don't try to move a subkey an everything subordinate, I need to save a subkey and everything subordinate and then later load the subkey and everything below this subkey, tell me your way. Thanks.

You can do it in regedit.  Click start, then run, type regedit.

In RegEdit, find the subkey, select the key, then click file, then click export registry file.  It can be imported through the file menu in regedit.

Saved registry files can be viewed using notepad.  WARNING: If you double-click a reg file, it will automatically be imported into your registry, so avoid this unless you want the registry changed.


mrmick: No, 'cause I need an automatic process. 8-( And of course, the Save process too.

If you are running on Windows NT, you need to set the SeRestorePrivilege before.


y96andha: I'n not running Window NT.


It might help if you post some of your code....  I have used both functions before without a problem.

RegRestoreKey will restore anything saved with RegSaveKey...


tward: Yes, you are right, but RegRestoreKey don't restore registry until the next reboot. And I need the new values at the moment. 8-(
I try to post some code.

You can in your program call regedit with a command line argument, which is text file containing all the keys.


faster: I can't make this thing 'cause in the environment that runs my application can be executed regedit. :-(

Have you tried to use regobj.dll (this is an activex server for registry manipulation)? This enables you to access and modify all the keys of the registry. I found it in the VB owner's area at www.microsoft.com.

You can use regobj to read the registry and store in a file  the registry keys and values to be restored.
When you need to restore these values, you can read the file and use regobj to modify the corresponding registry keys and values.

Top Expert 2009

I'd like to know why my answer was rejected

paolodp: Your answer was rejected 'cause in the system for the implentation we need to forgive ("olvidar") to install new DLLs and OCXs... 8-(


Tarod, are you wanting to save and create a key tree that you've created?  I could write you functions that would dump a key (and everything subordinate to a text file, and another function to restore the registry from the text file.  What's your email address?  What version of VB are you running?

mrmick: At this point my code is working in the way of dirtdart, what's your way to make the same thing in other maner? The best answer, yours or dirtdart's answer will win the points... :-)


I was going to offer to write you a procedure that works similar to the method dirtdart suggested.  Go ahead and award these points to dirtdart.

mrmick: Ok, a lot of thanks in anyway.


No problem.  Thank you.

dirtdart: Please, post an Answer I them I'll can give you the points. Thanks.


By the way, There isn't a better method than what dirtdart suggested, I hope you give him an "A".

mrmick: I think so. You are the fastest one! :-)


Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long

Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

Declare Function RegCloseKey Lib "advapi32.dll" Alias "RegCloseKey" (ByVal hKey As Long) As Long

I'm going to assume that you know how to use these functions.  If not, I will gladly give more explination about them.  Here's how I would use them, though:

Dim sKeys(255) As String ' Make an array of 255 values to hold the key names
Dim ct As Long 'Just A counter, but it has to have global scope

Private Sub EnumKeys()

Dim RetVal As Long
Dim phKey As Long
Dim lpBuffer As String
Dim nSize As Long

ct = 0

RegOpenKey HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows", phKey


lpBuffer = Space(255)
nSize = 255

RetVal = RegEnumKey phKey, ct, lpBuffer, nSize

    sKeys(ct) = lpBuffer
    ct = ct + 1
End If


RegCloseKey phKey

End Sub

At this point, You have all your key names in sKeys().  You can now do a loop just like you did above for each key and use RegEnumValue to get all the values associated with the keys.  There are quite a few ways to approach this, so instead of source code, I'll just outline them and let you make up your own mind about technique.

What I would probably do at this point is embed code in my loop to write each individual value to an ini file, using the key name as the Application name in the ini file.  This would give you a structure almost exactly like the registry that would then be easy to read back in and write back to the registry.

You could also use Open# and Print# to just write everything out to a file, but if you run into any special characters they would give you trouble.

Or, you could hold everything in variables.  If you're going to do that, I would make a two dimension array for the values.  This way, The first dimension of the array would be the same as the key that it's associated with, and the second dimension would enumerate the values.

Well, If I left anything out, or if you just need more information, let me know.  I'll be glad to help.
Thanks. But when I told: "post an Answer" I refer to a message (little message) then I can give you points. But thanks for the code anyway. ;-)
Thanks to other people that read the thread.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Ok.  Call it overkill.  But for 300 points (or the 1200 you gave me, thanks!), I just wanted to make sure you got your money's worth, and understood the answer.

Glad I could help out

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.