Solved

getting and updating regedit data through VB 6.0

Posted on 2008-06-13
11
179 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
  • 6
  • 5
11 Comments
 
LVL 19

Expert Comment

by:elimesika
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 19

Expert Comment

by:elimesika
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

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 19

Expert Comment

by:elimesika
Comment Utility
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
Comment Utility
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 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Nice , I like COM stuff ....

have FUN
0
 

Author Closing Comment

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

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

763 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

10 Experts available now in Live!

Get 1:1 Help Now