Microsoft KB Archive/297292

= How To Use RegObji.exe to Copy a Registry Key Programmatically =

Article ID: 297292

Article Last Modified on 8/5/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition

-



This article was previously published under Q297292



SUMMARY
This article serves two purposes:
 * 1) Demonstrate how to use RegObji.exe to copy a registry key to another location in the registry.
 * 2) Demonstrate how to use a registry key that contains binary data (specified as type REG_BINARY) because the examples that come with RegObji.exe do not demonstrate how to handle a binary registry value.

The code sample in this article does not store the REG_BINARY value. If you need to preserve a value by assigning it to a variable, you can store the binary data in a variable of the VARIANT datatype.

The following file is available for download from the Microsoft Download Center:

Download RegObji.exe now

For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to Obtain Microsoft Support Files from Online Services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file.



Step-by-Step Procedure
 Create a new Standard EXE project in Visual Basic. Form1 is created by default. On the Project menu, add a new standard Module, Module1, to the project. On the Project menu, click References, select Registration Manipulation Classes (RegObji.exe), and then click OK.  Add the following code to Module1: Option Explicit

Dim readReg As Registry Dim writeReg As Registry Dim keyExists As Boolean Dim I As Integer Dim wKeys As RegKey Dim rKeys As RegKey Dim hiveR As RegKey Dim hiveW As RegKey Dim KeyToAdd As String Dim KeyToFind As String

Public Sub main

KeyToAdd = &quot;Testing&quot; ' The following key should be present on all systems. KeyToFind = &quot;Microsoft\Direct3D\Drivers\Direct3D HAL&quot; Set writeReg = New Registry Set readReg = New Registry

Set readReg = New RegObj.Registry Set writeReg = New RegObj.Registry

Set hiveR = readReg.RegKeyFromString(&quot;\HKEY_LOCAL_MACHINE\SOFTWARE\&quot;) Set rKeys = hiveR.SubKeys(KeyToFind) Set hiveW = writeReg.RegKeyFromString(&quot;\HKEY_LOCAL_MACHINE\SOFTWARE\&quot;)

For I = 1 To hiveR.SubKeys.Count If hiveR.SubKeys(I).Name = KeyToAdd Then keyExists = True Exit For End If Next

' If the key already exists, do not modify it. ' This may be changed as needed. If Not keyExists Then hiveW.SubKeys.Add (KeyToAdd) hiveW.SubKeys.Reset

Set wKeys = hiveW.SubKeys(KeyToAdd)

For I = 1 To rKeys.Values.Count wKeys.Values.Add rKeys.Values(I).Name, rKeys.Values(I).Value, _ rKeys.Values(I).Type Next End If End Sub  On the Project menu, click Project1 Properties, and change the StartUp object to Sub Main. Run the project. To verify that a key named Testing is created under HKEY_LOCAL_MACHINE\SOFTWARE, run Regedit.exe. You may want to remove the Testing key when you are finished.</li></ol>

Additional query words: RegObj.dll

Keywords: kbhowto kbdownload kbregistry kbcodesnippet kbdll kbfile KB297292

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.