Solved

Commands for manipulating the Registry

Posted on 2000-05-17
11
198 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
[X]
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
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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
 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…

733 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