Commands for manipulating the Registry

Posted on 2000-05-17
Medium Priority
Last Modified: 2012-06-27
I would like to know what commands I can use to modify the registry using VB6 on an NT4 machine.  For example: I need to find out what the value at HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Service\El3c5741\Parameters\Tcpip\IPAddress is.  Then I want to change it and save the change.

I know that for WSH there are RegRead/Write commands, but I don't know their equivelant in VB.  I have found GetSettings but I don't think I can use this.  Am I wrong?

Thanks for the help,
Question by:robinsonbpc
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +3

Expert Comment

ID: 2817433
Here's a class I use that works as a wrapper around the Registry API's..


Expert Comment

ID: 2817437
Use ReadRegistry and WriteRegistry:
Public Function ReadRegistry(ByVal Key As String, ByVal Name As String, ByVal DefaultValue) As String
    Dim KeyHandle As Long
    Dim Result As Long
    Dim LType As Long
    Dim Data As String
    Dim DataLen As Long
    ReadRegistry = DefaultValue
    Result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, Key, 0, KEY_READ, KeyHandle)
    If (Result = ERROR_SUCCESS) Then
        DataLen = 500
        Data = String$(DataLen, 0)
        Result = RegQueryValueEx(KeyHandle, Name, 0, LType, ByVal Data, DataLen)
        If (Result = ERROR_SUCCESS) Then
            ReadRegistry = Left$(Data, DataLen - 1)
        End If
    End If
    Call RegCloseKey(KeyHandle)
End Function

Public Sub WriteRegistry(ByVal Section As String, ByVal Key As String, ByVal ValType As Integer, ByVal Value As Variant)
    Dim lResult As Long
    Dim lKeyValue As Long
    Dim InLen As Long
    Dim lNewVal As Long
    Dim sNewVal As String
    On Error Resume Next
    lResult = RegCreateKey(HKEY_LOCAL_MACHINE, Section, lKeyValue)
    If ValType = ValDWord Then
       lNewVal = CLng(Value)
       InLen = 4
       lResult = RegSetValueExLong(HKEY_LOCAL_MACHINE, Key, 0&, ValType, lNewVal, InLen)
       If ValType = ValString Then Value = Value & Chr(0)
       sNewVal = Value
       InLen = Len(sNewVal)
       lResult = RegSetValueExString(lKeyValue, Key, 0&, 1&, sNewVal, InLen)
    End If
    lResult = RegFlushKey(lKeyValue)
    lResult = RegCloseKey(lKeyValue)
    On Error GoTo 0
End Sub

You 'll need this information in a module:

Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Declare Function RegFlushKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long
Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long
LVL 28

Accepted Solution

AzraSound earned 800 total points
ID: 2817456
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 28

Expert Comment

ID: 2817464
you can use wsh as you said..go to project -> references and check off Windows Scripting Host Object Model

Expert Comment

ID: 2817522
If you use vb6, then add 'registry access functions' into your reference.

add the following code into your form

Option Explicit

Private Sub Command1_Click()
Dim x As New Registry

x.UpdateKey HKEY_CURRENT_USER, "Control panel\desktop", "Wallpaper", "your screensaver path"
Set x = Nothing
End Sub

Expert Comment

ID: 2817531
You can read and write to the registry using SaveSetting() and GetSetting().

Here's an example of writing:

Call SaveSetting(App.Title, "UserDetails", _
"Name", "Micky")
And reading:

MsgBox GetSetting(App.Title, "UserDetails", "Name")
Here's the syntax for each method:

Call SaveSetting(ApplicationName, SectionName, _
KeyName, Value)
MsgBox GetSetting(ApplicationName, SectionName, _
KeyName[, DefaultValue])
You can delve into the registry and view your stored values by running regedit.exe – but do so with caution and be careful not to delete anything you shouldn't!


Expert Comment

ID: 2817538
The following is sample Windows Scripting Host code to read and write from the registry:

  Private Sub WebClass_Start()
      Dim objWSH As Object
      Set objWSH = CreateObject("WScript.Shell")
      objWSH.RegWrite "HKLM\Software\MyApplication\strValue", "Some string value"
      objWSH.RegWrite "HKLM\Software\MyApplication\strReg\", 1, "REG_DWORD"

      Response.Write objWSH.RegRead("HKLM\Software\MyApplication\strValue") & "<P>"
      Response.Write objWSH.RegRead("HKLM\Software\MyApplication\strReg\") & "<P>"
      objWSH.RegDelete "HKLM\Software\MyApplication\strValue"
      objWSH.RegDelete "HKLM\Software\MyApplication\strReg\"
      objWSH.RegDelete "HKLM\Software\MyApplication\"
  End Sub


  You can download the Windows Scripting Host from the following location:


Author Comment

ID: 2822284
Adjusted points from 100 to 200

Author Comment

ID: 2822285
Thanks for all the suggestions.

The class from Vbmaster & the one on the site AzraSound suggested work best for my purposes.  However with both of these I am not able to read or write to binary entries in the registry.  Can somebody help with why this may be?  Here's what I'm doing based on using Vbmaster's class:

Private Sub Command1_Click()
   Dim c As New clsRegistry
   retVal = c.QueryValue(HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Services\El3c5741\Parameters\Tcpip", "IPAddress")

End Sub

That's a binary value.  If I do the same thing to a DWORD or String it works.

NOTE:  Please don't send instructions on how to do this using WSH.  I'm interested in how to do this using VB6.

Thanks again.
LVL 28

Expert Comment

ID: 2823110
check out the samples at the bottom of the page i linked you to

Author Comment

ID: 2825420
Thanks all for the help.

Azra, you were right about the examples on the site posted.  I had been having trouble making them work, but with a little manipulation I got them going.

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

800 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