• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

Commands for manipulating the Registry

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,
  • 3
  • 3
  • 2
  • +3
1 Solution
Here's a class I use that works as a wrapper around the Registry API's..

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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

you can use wsh as you said..go to project -> references and check off Windows Scripting Host Object Model
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
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!

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:

robinsonbpcAuthor Commented:
Adjusted points from 100 to 200
robinsonbpcAuthor Commented:
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.
check out the samples at the bottom of the page i linked you to
robinsonbpcAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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