Solved

Commands for manipulating the Registry

Posted on 2000-05-17
11
194 Views
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,
0
Comment
Question by:robinsonbpc
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 7

Expert Comment

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

www.cyd.liu.se/~freqv416/clsRegistry.cls
0
 
LVL 9

Expert Comment

by:GivenRandy
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)
    Else
       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
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 200 total points
ID: 2817456
0
 
LVL 28

Expert Comment

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

Expert Comment

by:AnswerTheMan
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
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Expert Comment

by:Simonac
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!

0
 
LVL 5

Expert Comment

by:AnswerTheMan
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


  REFERENCES

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

       http://www.microsoft.com/msdownload/vbscript/scripting.asp
0
 
LVL 1

Author Comment

by:robinsonbpc
ID: 2822284
Adjusted points from 100 to 200
0
 
LVL 1

Author Comment

by:robinsonbpc
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.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2823110
check out the samples at the bottom of the page i linked you to
0
 
LVL 1

Author Comment

by:robinsonbpc
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.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

706 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

12 Experts available now in Live!

Get 1:1 Help Now