Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 557
  • Last Modified:

How to restore a registry tree?

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
Tarod
Asked:
Tarod
  • 12
  • 6
  • 3
  • +5
1 Solution
 
dirtdartCommented:
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
 
TarodAuthor Commented:
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
 
dirtdartCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

0
 
TarodAuthor Commented:
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
 
mrmickCommented:
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
 
TarodAuthor Commented:
mrmick: No, 'cause I need an automatic process. 8-( And of course, the Save process too.

0
 
y96andhaCommented:
If you are running on Windows NT, you need to set the SeRestorePrivilege before.
0
 
TarodAuthor Commented:
y96andha: I'n not running Window NT.

0
 
twardCommented:
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
 
TarodAuthor Commented:
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
 
fasterCommented:
You can in your program call regedit with a command line argument, which is text file containing all the keys.
0
 
TarodAuthor Commented:
faster: I can't make this thing 'cause in the environment that runs my application can be executed regedit. :-(

0
 
paolodpCommented:
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
 
abelCommented:
I'd like to know why my answer was rejected
0
 
paolodpCommented:
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
 
TarodAuthor Commented:
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
 
mrmickCommented:
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
 
TarodAuthor Commented:
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
 
mrmickCommented:
mrmick: Ok, a lot of thanks in anyway.
0
 
TarodAuthor Commented:
No problem.  Thank you.
0
 
mrmickCommented:
dirtdart: Please, post an Answer I them I'll can give you the points. Thanks.

0
 
TarodAuthor Commented:
By the way, There isn't a better method than what dirtdart suggested, I hope you give him an "A".
0
 
mrmickCommented:
mrmick: I think so. You are the fastest one! :-)

0
 
TarodAuthor Commented:
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
 
dirtdartCommented:
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
 
TarodAuthor Commented:
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 12
  • 6
  • 3
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now