Results 1 to 8 of 8

Thread: RegOpenKey and RegCloseKey

  1. #1

    Thread Starter
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Exclamation

    Hi,
    Can any one explain the use of RegOpenKey and RegCloseKey in regarding to the Registry. I have noticed that some people use it in their code and some people dont.
    Is it necessary to use them, if so why ?

    Thanks in advance


  2. #2
    Guest
    Here is my full registry Module.

    Put the following in a Module.
    Code:
    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 RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal HKEY As Long, ByVal lpSubKey As String) As Long
    Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal HKEY As Long, ByVal lpValueName As String) As Long
    Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal HKEY As Long, ByVal lpSubKey As String, 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 RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal HKEY As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    Public Const HKEY_CLASSES_ROOT = &H80000000
    Public Const HKEY_CURRENT_USER = &H80000001
    Public Const HKEY_LOCAL_MACHINE = &H80000002
    Public Const HKEY_USERS = &H80000003
    Public Const HKEY_PERFORMANCE_DATA = &H80000004
    Public Const REG_SZ = 1
    
    Function RegQueryStringValue(ByVal HKEY As Long, ByVal strValueName As String)
        Dim lResult As Long
        Dim lValueType As Long
        Dim strBuf As String
        Dim lDataBufSize As Long
        
        On Error GoTo 0
        lResult = RegQueryValueEx(HKEY, strValueName, 0&, lValueType, ByVal 0&, lDataBufSize)
        If lResult = ERROR_SUCCESS Then
            If lValueType = REG_SZ Then
                strBuf = String(lDataBufSize, " ")
                lResult = RegQueryValueEx(HKEY, strValueName, 0&, 0&, ByVal strBuf, lDataBufSize)
                If lResult = ERROR_SUCCESS Then
                    RegQueryStringValue = StripTerminator(strBuf)
                End If
            End If
        End If
    End Function
    
    Public Function GetSettingEx(HKEY As Long, sPath As String, sValue As String)
        Dim KeyHand&
        Dim datatype&
        Call RegOpenKey(HKEY, sPath, KeyHand&)
        GetSettingEx = RegQueryStringValue(KeyHand&, sValue)
        Call RegCloseKey(KeyHand&)
    End Function
    
    Function StripTerminator(ByVal strString As String) As String
        Dim intZeroPos As Integer
    
        intZeroPos = InStr(strString, Chr$(0))
        If intZeroPos > 0 Then
            StripTerminator = Left$(strString, intZeroPos - 1)
        Else
            StripTerminator = strString
        End If
    End Function
    
    Public Sub SaveSettingEx(HKEY As Long, sPath As String, sValue As String, sData As String)
        Dim KeyHand As Long
        Call RegCreateKey(HKEY, sPath, KeyHand)
        Call RegSetValueEx(KeyHand&, sValue, 0, REG_SZ, ByVal sData, Len(sData))
        Call RegCloseKey(KeyHand&)
    End Sub
    Usage: Requires 2 Commandbuttons
    Code:
    Private Sub Command1_Click()
        'Save a Value to the Registry
        savestring HKEY_CURRENT_USER, "Software\Myapp", "Testing", "Hello"
    End Sub
    
    Private Sub Command2_Click()
        'Get a value from the Registry
        Retval = GetString(HKEY_CURRENT_USER, "Software\Myapp", "Testing")
        Print Retval
    End Sub

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    That's fine if you only want strings. To use DWORDs as well, here's the code, and an example: http://www.parksie.uklinux.net/registry.zip
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4

    Thread Starter
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    Thanks,
    But i actually wanted to know why do you have to use RegOpenKey and RegClose function and how come some people dont use it in their code.

    Thanks again

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    GetSetting and SaveSetting only allow changing of string values under HKEY_CURRENT_USER\Software\VB and VBA Program Settings. For many apps, that is fine, but other people want to give a professional edge: ie use Software\My Company\My Product\Options etc. The RegOpenKeyEx function and its siblings let you access any part of the registry (except on NT, where permissions may limit you). You can also enter/retrieve items of any type.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    Guest
    It's not that hard to save as DWORD's. Simply replace REG_SZ with REG_DWORD. You'll need this constant for it.
    Code:
    Const REG_DWORD = 4

  7. #7

    Thread Starter
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    I am sorry you guys misunderstood my question. I will try to explain again. Look at the following code, in here they dont use the Function RegOpenKey and RegCloseKey at all but the code still works.

    If you look at Megatron's Code you will see he used both function(regopenkey & regclosekey). I just wnat to know why do you have to use these two functions. Any ideas

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _
    4.     (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    5. Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" _
    6.     (ByVal hKey As Long, ByVal lpSubKey As String) As Long
    7. Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" _
    8.     (ByVal hKey As Long, ByVal lpValueName As String) As Long
    9. Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _
    10.     (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
    11.     lpType As Long, lpData As Any, lpcbData As Long) As Long
    12. Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
    13.     (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
    14.     ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    15.  
    16. Const ERROR_SUCCESS = 0&
    17. Const ERROR_BADDB = 1009&
    18. Const ERROR_BADKEY = 1010&
    19. Const ERROR_CANTOPEN = 1011&
    20. Const ERROR_CANTREAD = 1012&
    21. Const ERROR_CANTWRITE = 1013&
    22. Const ERROR_REGISTRY_RECOVERED = 1014&
    23. Const ERROR_REGISTRY_CORRUPT = 1015&
    24. Const ERROR_REGISTRY_IO_FAILED = 1016&
    25. Const HKEY_CLASSES_ROOT = &H80000000
    26. Const HKEY_CURRENT_USER = &H80000001
    27. Const HKEY_LOCAL_MACHINE = &H80000002
    28. Const REG_SZ = 1
    29.  
    30. Const regKey = "Sybex\Mastering VB 5.0"
    31.    
    32. Private Sub Form_Load()
    33. Dim retValue As Long
    34. Dim result As Long
    35. Dim keyID As Long
    36. Dim keyValue As String
    37. Dim subKey As String
    38. Dim bufSize As Long
    39.    
    40.     Label6.Caption = regKey
    41.     'Create key
    42.     retValue = RegCreateKey(HKEY_CURRENT_USER, regKey, keyID)
    43.     If retValue = 0 Then
    44.         'Create width
    45.         subKey = "Window Width"
    46.         retValue = RegQueryValueEx(keyID, subKey, 0&, REG_SZ, _
    47.                    0&, bufSize)
    48.         'No value, set it
    49.         If bufSize < 2 Then
    50.             keyValue = Me.Width
    51.             retValue = RegSetValueEx(keyID, subKey, 0&, _
    52.                                         REG_SZ, ByVal keyValue, Len(keyValue) + 1)
    53.         Else
    54.                                    
    55.             keyValue = String(bufSize + 1, " ")
    56.             retValue = RegQueryValueEx(keyID, subKey, 0&, REG_SZ, _
    57.                        ByVal keyValue, bufSize)
    58.             keyValue = Left$(keyValue, bufSize - 1)
    59.             Me.Width = keyValue
    60.         End If
    61.         'Set values on form
    62.         Label4.Caption = subKey
    63.         Label5.Caption = Me.Width
    64.         'Create height
    65.         subKey = "Window Height"
    66.         retValue = RegQueryValueEx(keyID, subKey, 0&, REG_SZ, _
    67.                    0&, bufSize)
    68.         If bufSize < 2 Then
    69.             keyValue = Me.Height
    70.             retValue = RegSetValueEx(keyID, subKey, 0&, _
    71.                        REG_SZ, ByVal keyValue, Len(keyValue) + 1)
    72.         Else
    73.             keyValue = String(bufSize + 1, " ")
    74.             retValue = RegQueryValueEx(keyID, subKey, 0&, REG_SZ, _
    75.                        ByVal keyValue, bufSize)
    76.             keyValue = Left$(keyValue, bufSize - 1)
    77.             Me.Height = keyValue
    78.         End If
    79.         'Set values on form
    80.         Label8.Caption = subKey
    81.         Label7.Caption = Me.Height
    82.     End If
    83. End Sub
    84.  
    85. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    86. Dim keyValue As String
    87. Dim retValue As Long
    88. Dim keyID As Long
    89.    
    90.     retValue = RegCreateKey(HKEY_CURRENT_USER, regKey, keyID)
    91.     keyValue = Me.Width
    92.     retValue = RegSetValueEx(keyID, "Window Width", 0&, _
    93.                REG_SZ, ByVal keyValue, Len(keyValue) + 1)
    94.     keyValue = Me.Height
    95.     retValue = RegSetValueEx(keyID, "Window Height", 0&, _
    96.                REG_SZ, ByVal keyValue, Len(keyValue) + 1)
    97.     MsgBox "Registry updated"
    98. End Sub
    Last edited by Danial; Aug 1st, 2001 at 09:19 PM.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    RegCreateKeyEx creates a key and opens it. If the key is already there, it simply opens it. RegOpenKeyEx, on the other hand, returns an error if the key is not present. In the example you have, their code is wrong, because you should always close every key you open.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width