Tip

Class Registry

Get/Set Windows registry values
'ClassRegistry:

Option Public
Option Explicit




'// API Constants
Const REG_SZ = 1
Const REG_DWORD = 4

Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003

Const ERROR_NONE = 0
Const ERROR_BADDB = 1
Const ERROR_BADKEY = 2
Const ERROR_CANTOPEN = 3
Const ERROR_CANTREAD = 4
Const ERROR_CANTWRITE = 5
Const ERROR_OUTOFMEMORY = 6
Const ERROR_INVALID_PARAMETER = 7
Const ERROR_ACCESS_DENIED = 8
Const ERROR_INVALID_PARAMETERS = 87
Const ERROR_NO_MORE_ITEMS = 259

Const KEY_ALL_ACCESS = &H3F
Const REG_OPTION_NON_VOLATILE = 0
Const REG_PARAMETER_IS_RESERVED = 0
Const REG_DEFAULT_VALUE = ""

Const cMAXLEN_BUFFER = 255

'// API Declarations
Declare Function RegCloseKey Lib "advapi32.dll" (Byval hKey As Long) As Long
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
Declare Function RegQueryValueExString Lib "advapi32.dll" Alias
"RegQueryValueExA" (Byval hKey As Long, Byval lpValueName As String, Byval
lpReserved As Long, lpType As Long, Byval lpData As String, lpcbData As Long)
As Long
Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias
"RegQueryValueExA" (Byval hKey As Long, Byval lpValueName As String, Byval
lpReserved As Long, lpType As Long, lpData As Long, lpcbData As Long) As Long
Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias
"RegQueryValueExA" (Byval hKey As Long, Byval lpValueName As String, Byval
lpReserved As Long, lpType As Long, Byval lpData As Long, lpcbData As Long) As
Long
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
Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA"
(Byval hKey As Long, Byval lpValueName As String, Byval Reserved As Long, Byval
dwType As Long, lpValue As Long, Byval cbData As Long) As Long


Const cREG_COMPONENTS_PATH = "Software\Lotus\Lotus Components\1.0"
Const cREG_COMPONENTS_KEYNAME = "LoaderPath"

Class Registry

'// Declare Member Varaibles
Private hkey As Long

'// Declare Member Code
Declare Public Function GetValue( ItemName$ ) As String
Declare Public Function SetValue( ValueName As String, lType As Long, vValue As
Variant) As Long

'// Constructor
Sub New( Section, KeyName$ )
Call RegOpenKeyEx(Section, KeyName$, REG_PARAMETER_IS_RESERVED,
KEY_ALL_ACCESS, Me.hKey)
End Sub

'// Destructor
Sub Delete()
Call RegCloseKey (Me.hKey)
End Sub

Public Function GetValue( ItemName$ ) As String
Dim sData As String
Dim lpcbData As Long
Dim lpType As Long

'// First we initialize the buffer
sData = String(cMAXLEN_BUFFER, 0)
lpcbData = Len(sData)

'// Get it
Call RegQueryValueExString( Me.hKey, ItemName$,
REG_PARAMETER_IS_RESERVED, lpType, sData, lpcbData)

'// Return
GetValue = Left( sData, Instr( 1, sData, Chr$(0) )-1)


End Function

Public Function SetValue( ValueName As String, lType As Long, vValue As
Variant) As Long
Dim lValue As Long
Dim sValue As String

Select Case lType
Case REG_SZ

'//sValue gets assigned the value vValue
sValue = vValue

'// Set value to Windows

This was first published in November 2000

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.