Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

getting and updating regedit data through VB 6.0

Posted on 2008-06-13
11
Medium Priority
?
190 Views
Last Modified: 2011-10-19
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?
0
Comment
Question by:siva_gk
[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
  • 6
  • 5
11 Comments
 
LVL 19

Expert Comment

by:elimesika
ID: 21776791
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
 
LVL 19

Expert Comment

by:elimesika
ID: 21776833
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
 

Author Comment

by:siva_gk
ID: 21776844
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 19

Expert Comment

by:elimesika
ID: 21776930
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
 

Author Comment

by:siva_gk
ID: 21777166
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
 
LVL 19

Expert Comment

by:elimesika
ID: 21777290
HI

please send me your project (zipped + rename any illegal extension by adding .txt) and I will try to help you resolve it.
0
 

Author Comment

by:siva_gk
ID: 21777448
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
 
LVL 19

Accepted Solution

by:
elimesika earned 1000 total points
ID: 21777724
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
 

Author Comment

by:siva_gk
ID: 21777803
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
 
LVL 19

Expert Comment

by:elimesika
ID: 21777840
Nice , I like COM stuff ....

have FUN
0
 

Author Closing Comment

by:siva_gk
ID: 31466852
there are lot of stuffs in the example. anybody can play with regedit using this
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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 …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

721 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