getting and updating regedit data through VB 6.0

Hi everybody,

I am doing a VB project in which I have a textbox(txtData) 2 command buttons (cmdGet and CmdSet).

On cmdGet_Click, i want to display the regedit data of HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\HOME1 - FORMS60_PATH in the textbox.

On cmdSet_Click, i want to update the text1.text value in HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\HOME1 - FORMS60_PATH

anybody have an idea on this?
siva_gkAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
elimesikaConnect With a Mentor Commented:
HI

Sorry, since I work with VS 2008 I have problems to use your code (MS migration failed to convert the code)

However, I have found you a complete VB project with a form and examples for using set/get

Hope that it helps
VB6-Project.zip
0
 
elimesikaCommented:
see attached code
there is more info also in http://www.windowsdevcenter.com/pub/a/windows/2004/06/15/VB_Registry_Keys.html?page=3
Private 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
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As _
    Long
Private 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
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As _
    Any, source As Any, ByVal numBytes As Long)
 
Const KEY_READ = &H20019  ' ((READ_CONTROL Or KEY_QUERY_VALUE Or 
                          ' KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not 
                          ' SYNCHRONIZE))
 
Const REG_SZ = 1
Const REG_EXPAND_SZ = 2
Const REG_BINARY = 3
Const REG_DWORD = 4
Const REG_MULTI_SZ = 7
Const ERROR_MORE_DATA = 234
 
' Read a Registry value
'
' Use KeyName = "" for the default value
' If the value isn't there, it returns the DefaultValue
' argument, or Empty if the argument has been omitted
'
' Supports DWORD, REG_SZ, REG_EXPAND_SZ, REG_BINARY and REG_MULTI_SZ
' REG_MULTI_SZ values are returned as a null-delimited stream of strings
' (VB6 users can use SPlit to convert to an array of string)
 
Function GetRegistryValue(ByVal hKey As Long, ByVal KeyName As String, _
    ByVal ValueName As String, Optional DefaultValue As Variant) As Variant
    Dim handle As Long
    Dim resLong As Long
    Dim resString As String
    Dim resBinary() As Byte
    Dim length As Long
    Dim retVal As Long
    Dim valueType As Long
    
    ' Prepare the default result
    GetRegistryValue = IIf(IsMissing(DefaultValue), Empty, DefaultValue)
    
    ' Open the key, exit if not found.
    If RegOpenKeyEx(hKey, KeyName, 0, KEY_READ, handle) Then
        Exit Function
    End If
    
    ' prepare a 1K receiving resBinary
    length = 1024
    ReDim resBinary(0 To length - 1) As Byte
    
    ' read the registry key
    retVal = RegQueryValueEx(handle, ValueName, 0, valueType, resBinary(0), _
        length)
    ' if resBinary was too small, try again
    If retVal = ERROR_MORE_DATA Then
        ' enlarge the resBinary, and read the value again
        ReDim resBinary(0 To length - 1) As Byte
        retVal = RegQueryValueEx(handle, ValueName, 0, valueType, resBinary(0), _
            length)
    End If
    
    ' return a value corresponding to the value type
    Select Case valueType
        Case REG_DWORD
            CopyMemory resLong, resBinary(0), 4
            GetRegistryValue = resLong
        Case REG_SZ, REG_EXPAND_SZ
            ' copy everything but the trailing null char
            resString = Space$(length - 1)
            CopyMemory ByVal resString, resBinary(0), length - 1
            GetRegistryValue = resString
        Case REG_BINARY
            ' resize the result resBinary
            If length <> UBound(resBinary) + 1 Then
                ReDim Preserve resBinary(0 To length - 1) As Byte
            End If
            GetRegistryValue = resBinary()
        Case REG_MULTI_SZ
            ' copy everything but the 2 trailing null chars
            resString = Space$(length - 2)
            CopyMemory ByVal resString, resBinary(0), length - 2
            GetRegistryValue = resString
        Case Else
            RegCloseKey handle
            Err.Raise 1001, , "Unsupported value type"
    End Select
    
    ' close the registry key
    RegCloseKey handle
End Function

Open in new window

0
 
elimesikaCommented:
HI Again

Creating and setting a value of a specified key can be accomplished with the following short procedure. SetKeyValue takes the key that the value will be associated with, the name of the value, the setting of the value, and the type of the value (the SetValueEx function only supports REG_SZ and REG_DWORD, but this can be modified if necessary). Specifying a new value for an existing sValueName will modify the current setting of that value.
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
 
   Private Sub SetKeyValue (sKeyName As String, sValueName As String, _
   vValueSetting As Variant, lValueType As Long)
       Dim lRetVal As Long         'result of the SetValueEx function
       Dim hKey As Long         'handle of open key
 
       'open the specified key
       lRetVal = RegOpenKeyEx(HKEY_CURRENT_USER, sKeyName, 0, _
                                 KEY_SET_VALUE, hKey)
       lRetVal = SetValueEx(hKey, sValueName, lValueType, vValueSetting)
       RegCloseKey (hKey)
   End Sub
				

Open in new window

0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
siva_gkAuthor Commented:
thanks elimesika.

can you briefly eloborate about the parameters used in GetRegistryValue? and also let me know what I need to pass for each parametesr to obtain the value of HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\HOME1\FORMS60_PATH?
0
 
elimesikaCommented:
HI

In your case :

GetRegistryValue("HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\HOME1\","FORMS60_PATH","")

1st arg - path to the key

2nd arg - key name

3rd arg - the name of the value you want if there is more than 1 value per key

keep in mind that in the ("HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\HOME1\" the \ may be recognized as special char if it is not working use:

("HKEY_LOCAL_MACHINE\\SOFTWARE\\ORACLE\\HOME1\\"
0
 
siva_gkAuthor Commented:
ok. but i am getting Type mismatch error for this line.

Text1.Text = GetRegistryValue("HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\HOME1\\", "FORMS60_PATH", "")

I tried for single slash also.

can you tell me why?
0
 
elimesikaCommented:
HI

please send me your project (zipped + rename any illegal extension by adding .txt) and I will try to help you resolve it.
0
 
siva_gkAuthor Commented:
elimesika, please find the attachment.

there will be 2 textboxes and 2 command buttons.

one for getting the value of FORMS60_PATH and the other for updating/setting value for FORMS60_PATH

please help me!!!
extract-and-extract.zip
0
 
siva_gkAuthor Commented:
Yeah, thanks elimesika. It seems to be a good stuff for playing with Regedit. In meanwhile  came up with a solution for my requirement (getting and changing Forms path)  with a simple coding. please have a look.

Option Explicit
 
Const FormsPath = "HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\HOME1\FORMS60_PATH"
 
Private Sub cmdGetPath_Click()
Dim objCreate As Object
Set objCreate = CreateObject("wscript.shell")
 
 
On Error GoTo ErrHandler
Text1.Text = objCreate.regread(FormsPath)
 
Exit Sub
ErrHandler:
MsgBox Err.Description, vbInformation, Err.Number
 
End Sub
 
Private Sub cmdSetPath_Click()
Dim objCreate As Object
Set objCreate = CreateObject("wscript.shell")
 
On Error GoTo ErrHandler
objCreate.RegWrite FormsPath, Text2.Text, "REG_SZ"
 
Exit Sub
ErrHandler:
MsgBox Err.Description, vbInformation, Err.Number
End Sub

Open in new window

0
 
elimesikaCommented:
Nice , I like COM stuff ....

have FUN
0
 
siva_gkAuthor Commented:
there are lot of stuffs in the example. anybody can play with regedit using this
0
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.

All Courses

From novice to tech pro — start learning today.