Solved

Export/Import registry keys

Posted on 2001-06-14
6
237 Views
Last Modified: 2012-06-27
Hi,

Does anyone have some code that will export all keys and data in a known registry branch to file ie

HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Auto-BackupFTP

as well as recreate all Keys when importing again please.

Thank you.

Regards,
    Tony
0
Comment
Question by:tvtech
  • 2
  • 2
  • 2
6 Comments
 
LVL 3

Expert Comment

by:RodStephens
ID: 6190918
If you don't need to do this with a VB program, the Registry Editor will do it.

Use Start/Run/regedit to start it. Select the branch you want to export. From the File menu, select Export Registry File. in the file selection dialog, make sure the Select Branch option is selected and that the right branch is filled in.

The Registry Editor will import a saved branch similarly through the file menu, or just double click on the .reg file.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6190984
Private Sub ReadSettings()
    Open "c:\testbed\Auto-BackupFTP.reg" For Input As #1
    Do
        Line Input #1, strLine
        arySettings = Split(strLine, "=")
        SaveSetting "Auto-BackupFTP ", "Auto-BackupFTP ", arySettings(0), arySettings(1)
    Loop Until EOF(1)
    Close #1
End Sub

Private Sub WriteSettings()
    Dim aryRegistry()
    Dim varRegistry As Variant
    varRegistry = GetAllSettings("Auto-BackupFTP ", "Auto-BackupFTP ")
'    MsgBox UBound(varRegistry)
    Open "c:\testbed\Auto-BackupFTP.reg" For Output As #1
    For intKey = LBound(varRegistry) To UBound(varRegistry)
        Print #1, varRegistry(intKey, 0) & "=" & varRegistry(intKey, 1)
    Next
    Close #1
End Sub

Should get you started, though it only does it for a specific section at a time.
0
 

Author Comment

by:tvtech
ID: 6194914
Thanks TimCottee,

Your answer is on the right track, but looking at my Post, I realised that I had missed the fact that there are many sub Keys (Child nodes) within the Parent key and the code is not recursive. Any ideas?

Thanks again.

Regards,
   Tony

PS - Thanks Rod, but Regedit is not an option. Code is the only path I can take.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 3

Expert Comment

by:RodStephens
ID: 6195241
> PS - Thanks Rod, but Regedit is not an option. Code is the only path I can take.

Gotta try the easy solutioun first ;-)

Take a look at this example. It doesn't save the information into a file, but it recursively traverses it. You can write it into a file. Use regedit to export a piece of registry to see what the format should look like.

To load the file, I think you can use AppExecute to "open" it, though that will probably make a dialog appear. Or you can dig through the file yourself creating the keys.

Nto a complete solution, but it should get you started.
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 250 total points
ID: 6195254
tvtech,

Here is a sample which should achieve your aim, basically it uses the API functions which allow registry access to enumerate the keys and to save the results to a file

Private Const HKEY_CURRENT_CONFIG = &H80000005
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const HKEY_CURRENT_USER = &H80000001
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As Any) As Long
Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long

Private Sub Command1_Click()
    WriteSettings "c:\testbed\AllSettings.reg", HKEY_CURRENT_USER, "Software\VB And VBA Program Settings\Auto-BackupFTP ", List1
'Specify the file name, the root key that you want to use, and an optional list box on the form to display the results of the enumeration.
End Sub

Private Function StripTerminator(sInput As String) As String
    Dim ZeroPos As Integer
    'Search the first chr$(0)
    ZeroPos = InStr(1, sInput, vbNullChar)
    If ZeroPos > 0 Then
        StripTerminator = Left$(sInput, ZeroPos - 1)
    Else
        StripTerminator = sInput
    End If
End Function

Private Sub EnumKeys(ByVal FileName As String, ByVal HiveName As Long, ByVal KeyName As String, Optional ByVal Level As Integer = 0, Optional ListDisplay As ListBox)
    Dim hKey As Long, Cnt As Long, sSave As String
    RegOpenKey HiveName, KeyName, hKey
    EnumValues FileName, HiveName, KeyName, Level + 1, ListDisplay
    Do
        sSave = String(255, 0)
        If RegEnumKeyEx(hKey, Cnt, sSave, 255, 0, vbNullString, ByVal 0&, ByVal 0&) <> 0 Then Exit Do
        If Not (ListDisplay Is Nothing) Then ListDisplay.AddItem String(Level, vbTab) & StripTerminator(sSave)
        EnumKeys FileName, HiveName, KeyName & "\" & StripTerminator(sSave), Level + 1, ListDisplay
        Cnt = Cnt + 1
    Loop
    RegCloseKey hKey
End Sub

Private Sub EnumValues(ByVal FileName As String, ByVal HiveName As Long, ByVal KeyName As String, Optional ByVal Level As Integer = 0, Optional ListDisplay As ListBox)
    Dim hKey As Long, Cnt As Long, sSave As String
    Dim AppName As String, Section As String, Key As String, Value As Variant
    RegOpenKey HiveName, KeyName, hKey
    Do
        sSave = String(255, 0)
        If RegEnumValue(hKey, Cnt, sSave, 255, 0, ByVal 0&, ByVal 0&, ByVal 0&) <> 0 Then Exit Do
        intPos1 = InStr(Replace(KeyName, "Software\VB And VBA Program Settings", "") & "\", "\")
        intPos2 = InStr(intPos1 + 1, Replace(KeyName, "Software\VB And VBA Program Settings", "") & "\", "\")
        AppName = Mid(Replace(KeyName, "Software\VB And VBA Program Settings", ""), intPos1 + 1, intPos2 - intPos1 - 1)
        intPos1 = intPos2 + 1
        intPos2 = Len(KeyName) 'InStr(intPos1, Replace(KeyName, "Software\VB And VBA Program Settings", "") & "\", "\")
        Section = Mid(Replace(KeyName, "Software\VB And VBA Program Settings", ""), intPos1, intPos2 - intPos1)
        varValue = GetSetting(AppName, Section, StripTerminator(sSave), "N/A")
        If Not (ListDisplay Is Nothing) Then ListDisplay.AddItem String(Level, vbTab) & "* " & StripTerminator(sSave) & "=" & varValue
        If FileName <> "" Then
            Open "c:\testbed\Settings.reg" For Append As #1
            Print #1, AppName & "," & Section & "," & StripTerminator(sSave) & "," & varValue
            Close #1
        End If
        Cnt = Cnt + 1
    Loop
    RegCloseKey hKey
End Sub

Private Sub WriteSettings(FileName As String, HiveName As Long, KeyName As String, Optional ListDisplay As ListBox)
    If Not (ListDisplay Is Nothing) Then ListDisplay.Clear
    On Error Resume Next
    'Remove the file if it exists, this does not handle any error if the file is open elsewhere!
    Kill FileName
    EnumKeys FileName, HiveName, KeyName, 0, ListDisplay
End Sub

Private Sub ReadSettings(FileName As String)
    Dim AppName As String, Section As String, Key As String, Value As Variant
    Open FileName For Input As #1
    Do
        Input #1, AppName, Section, Key, Value
        SaveSetting AppName, Section, Key, Value
    Loop Until EOF(1)
    Close #1
End Sub
0
 

Author Comment

by:tvtech
ID: 7029352
A belated thank you. :)

Regards,
Tony
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

705 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