Reading and Writing to the Registry [Source]

Discussion in 'Archives' started by Covey, Jun 29, 2007.

Reading and Writing to the Registry [Source]
  1. Unread #1 - Jun 29, 2007 at 7:45 AM
  2. Covey
    Joined:
    Sep 9, 2005
    Posts:
    4,510
    Referrals:
    9
    Sythe Gold:
    9
    Discord Unique ID:
    807246764155338833
    Discord Username:
    Covey#1816

    Covey Creator of EliteSwitch
    Retired Sectional Moderator Visual Basic Programmers

    Reading and Writing to the Registry [Source]

    This isn't my code put its usefull, very easy to use if you know how the registry works. I've taken my error handler out of it, so you may want to add your own back in

    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 REG_SZ = 1, REG_DWORD = 4
    Public Const HKEY_CLASSES_ROOT = &H80000000, HKEY_CURRENT_USER = &H80000001, HKEY_LOCAL_MACHINE = &H80000002, HKEY_USERS = &H80000003, HKEY_PERFORMANCE_DATA = &H80000004, ERROR_SUCCESS = 0&
    Option Explicit
    
    Public Function getstring(hKey As Long, strPath As String, strValue As String)
        Dim keyhand As Long, datatype As Long, lResult As Long, strBuf As String, lDataBufSize As Long, lValueType As Long, intZeroPos As Integer, r As Long
        r = RegOpenKey(hKey, strPath, keyhand)
        lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)
        If lValueType = REG_SZ Then
            strBuf = String(lDataBufSize, " ")
            lResult = RegQueryValueEx(keyhand, strValue, 0&, 0&, ByVal strBuf, lDataBufSize)
            If lResult = ERROR_SUCCESS Then
                intZeroPos = InStr(strBuf, Chr$(0))
                If intZeroPos > 0 Then
                    getstring = Left$(strBuf, intZeroPos - 1)
                Else
                    getstring = strBuf
                End If
            End If
        End If
    End Function
    
    
    Public Sub savestring(hKey As Long, strPath As String, strValue As String, strData As String)
        Dim keyhand As Long, r As Long
        r = RegCreateKey(hKey, strPath, keyhand)
        r = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strData, Len(strData))
        r = RegCloseKey(keyhand)
    End Sub
    How to write to the registry:
    This will save the contents of text1 to the registry.
    Code:
    Call savestring(HKEY_CURRENT_USER, "Sotware\VBW\Registry", "String", text1.text)
    How to read the registry:
    This will read the contents of the registry.
    Code:
    text1.text = getstring(HKEY_CURRENT_USER, "Software\VBW\Registry", "String")
    Chances are if you didn't understand any of this you would have know use for it, and i know my explanation is pretty sad but im sure Jazz will elaborate if you have any questions ;)
     
  3. Unread #2 - Jun 29, 2007 at 8:08 AM
  4. Jazz00006
    Joined:
    Aug 26, 2005
    Posts:
    807
    Referrals:
    0
    Sythe Gold:
    0

    Jazz00006 Apprentice
    Visual Basic Programmers

    Reading and Writing to the Registry [Source]

    What this? a recommendation from covey :D

    And I hate to burst your bubble, but
    1) VB has its own, inbuilt reg write functions
    2) All that code is not needed, see below:

    Code:
    Dim SScript
    Set SScript = CreateObject("WScript.Shell")
    Simplified version of the declerations


    Code:
    SScript.RegWrite "HKLM\Software\JazzBuddy", "test"
    Write a value into the registry under HKEY Local Machine

    Code:
    MsgBox SScript.RegRead("HKLM\Software\JazzBuddy")
    Spit out that value, this can be placed into a value of your choice as well.
     
  5. Unread #3 - Jun 29, 2007 at 12:28 PM
  6. Covey
    Joined:
    Sep 9, 2005
    Posts:
    4,510
    Referrals:
    9
    Sythe Gold:
    9
    Discord Unique ID:
    807246764155338833
    Discord Username:
    Covey#1816

    Covey Creator of EliteSwitch
    Retired Sectional Moderator Visual Basic Programmers

    Reading and Writing to the Registry [Source]

    Wow, how embarassing.
    I'd hate to be that covey fellow right now :p
     
  7. Unread #4 - Jun 30, 2007 at 5:25 AM
  8. Darthatron
    Joined:
    May 22, 2006
    Posts:
    1,612
    Referrals:
    3
    Sythe Gold:
    0

    Darthatron Massive Troll
    Retired Sectional Moderator Visual Basic Programmers

    Reading and Writing to the Registry [Source]

    Haha, wow. I used Covey's function a while back for trail version I made, I had no idea it was so easy... I feel silly... Nice find Jazz.
     
< Selling PBP! | ~~selling account names!~~ >

Users viewing this thread
1 guest


 
 
Adblock breaks this site