Results 1 to 5 of 5

Thread: Newbie question about adding keys to the registry.

  1. #1

    Thread Starter
    Addicted Member AmmerBow's Avatar
    Join Date
    Sep 2000
    Posts
    195
    Just got out of the VB fund 6.0 class and have been searching awhile for the great wisdom of making changes to the registry via VB 6.0 (Not script).

    I want to assign a registry key entry to a varible then use my program to edit it and save it back.

    How on earth do I do this. what do i add, etc...

  2. #2
    Guest
    Add the following to 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:
    Code:
    'Save a value to the Registry
    SaveSettingEx HKEY_CURRENT_USER, "Software\Myapp", "Testing", "Hello"
    
    'Get a value from the Registry
    Retval = GetSettingEx(HKEY_CURRENT_USER, "Software\Myapp", "Testing")
    Print Retval

  3. #3

    Thread Starter
    Addicted Member AmmerBow's Avatar
    Join Date
    Sep 2000
    Posts
    195

    WOW

    All that for something that should be simple.

  4. #4
    Guest
    The above code will allow to you customize where you want to save. This next method will only let you save in a specific section.

    Code:
    'Save value
    SaveSetting "AppName", "Section", "Key", "Setting"
    
    'Get value
    Retval = GetSetting("AppName", "Section", "Key")
    This method is much shorter, however, it's not flexible. You are very limited as to where you want to save and what format you want to save.

  5. #5

    Thread Starter
    Addicted Member AmmerBow's Avatar
    Join Date
    Sep 2000
    Posts
    195

    mistake maybe?

    on this part of code:

    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
    *****************************
    The program reaches ERROR_SUCCESS and gives a "Variable not defined" error. What happened?

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