Solved

getting and updating regedit data through VB 6.0

Posted on 2008-06-13
11
180 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb6 programatically bring window to front 8 63
VBA open file from excel cell 4 36
Adding to a VBA? 6 61
Passing a Text Box name to a Sub 6 74
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

912 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

22 Experts available now in Live!

Get 1:1 Help Now