• Log in or sign up for an account here: https://www.sythe.org/login

Reading and Writing to the Registry [Source]

Covey

Creator of EliteSwitch

Retired Sectional Moderator
Visual Basic Programmers
Joined
Sep 9, 2005
Sythe Gold
9
Messages
4,510
Age
17
Location
Fuckin' BAM!
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 ;)
 
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.
 
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.
 
Runescape Gamblingrunescape gambling
Back
Top