Solved

How to restore a registry tree?

Posted on 1997-11-17
27
513 Views
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-(
0
Comment
Question by:Tarod
  • 12
  • 6
  • 3
  • +5
27 Comments
 
LVL 5

Expert Comment

by:dirtdart
ID: 1441168
If you used RegSaveKey to save a registry key, then you would need to use RegLoadKey to load it back in to the registry.
0
 

Author Comment

by:Tarod
ID: 1441169
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.
0
 
LVL 5

Expert Comment

by:dirtdart
ID: 1441170
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.
0
 
LVL 8

Expert Comment

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

0
 

Author Comment

by:Tarod
ID: 1441172
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.

0
 
LVL 8

Expert Comment

by:mrmick
ID: 1441173
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.

0
 

Author Comment

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

0
 
LVL 5

Expert Comment

by:y96andha
ID: 1441175
If you are running on Windows NT, you need to set the SeRestorePrivilege before.
0
 

Author Comment

by:Tarod
ID: 1441176
y96andha: I'n not running Window NT.

0
 
LVL 7

Expert Comment

by:tward
ID: 1441177
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...
0
 

Author Comment

by:Tarod
ID: 1441178
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.

0
 
LVL 7

Expert Comment

by:faster
ID: 1441179
You can in your program call regedit with a command line argument, which is text file containing all the keys.
0
 

Author Comment

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

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Expert Comment

by:paolodp
ID: 1441181
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.
 


0
 
LVL 39

Expert Comment

by:abel
ID: 1441182
I'd like to know why my answer was rejected
0
 

Expert Comment

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

0
 

Author Comment

by:Tarod
ID: 1441184
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?
0
 
LVL 8

Expert Comment

by:mrmick
ID: 1441185
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... :-)

0
 

Author Comment

by:Tarod
ID: 1441186
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.
0
 
LVL 8

Expert Comment

by:mrmick
ID: 1441187
mrmick: Ok, a lot of thanks in anyway.
0
 

Author Comment

by:Tarod
ID: 1441188
No problem.  Thank you.
0
 
LVL 8

Expert Comment

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

0
 

Author Comment

by:Tarod
ID: 1441190
By the way, There isn't a better method than what dirtdart suggested, I hope you give him an "A".
0
 
LVL 8

Expert Comment

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

0
 

Author Comment

by:Tarod
ID: 1441192
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

Do While RetVal <> ERROR_NO_MORE_ITEMS

lpBuffer = Space(255)
nSize = 255

RetVal = RegEnumKey phKey, ct, lpBuffer, nSize

If RetVal <> ERROR_NO_MORE_ITEMS Then
    sKeys(ct) = lpBuffer
    ct = ct + 1
End If

Loop

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.
0
 
LVL 5

Accepted Solution

by:
dirtdart earned 300 total points
ID: 1441193
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.
0
 

Author Comment

by:Tarod
ID: 1441194
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

Drew
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now