Commands for manipulating the Registry

Posted on 2000-05-17
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
  • 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 200 total points
ID: 2817456
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

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…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

773 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