PDA

Click to See Complete Forum and Search --> : Registry Editing


Kevin Seiden
Mar 3rd, 2001, 01:33 AM
I'm trying to make an easy to use module for editing the registry. I want to be able to save settings of any data type with one command. Here is what I've got so far, but I keep getting a type mismatch error on the CByte statement. If I comment out that line, the other two data types work fine. What am i doing wrong?

Public Enum ValueType
RegString
RegDWORD
RegBinary
End Enum

Private Const REG_SZ = 1 'Unicode nul terminated string
Private Const REG_BINARY = 3 'Free form binary
Private Const REG_DWORD = 4 '32-bit number

Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hkey As Long) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private 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 Sub SaveRegSetting(ByVal hkey As Long, ByVal strPath As String, ByVal strValue As String, ByVal varData As Variant, ByVal valDataType As ValueType)
Select Case valDataType
Case RegString
SaveSettingString hkey, strPath, strValue, CStr(varData)
Case RegDWORD
SaveSettingLong hkey, strPath, strValue, CLng(varData)
Case RegBinary
SaveSettingByte hkey, strPath, strValue, CByte(varData)
End Select
End Sub

Private Sub SaveSettingString(hkey As Long, strPath As String, strValue As String, strData As String)
Dim hCurKey As Long
Dim lRegResult As Long

lRegResult = RegCreateKey(hkey, strPath, hCurKey)
lRegResult = RegSetValueEx(hCurKey, strValue, 0, REG_SZ, ByVal strData, Len(strData))
lRegResult = RegCloseKey(hCurKey)
End Sub

Private Sub SaveSettingLong(ByVal hkey As Long, ByVal strPath As String, ByVal strValue As String, ByVal lData As Long)
Dim hCurKey As Long
Dim lRegResult As Long

lRegResult = RegCreateKey(hkey, strPath, hCurKey)
lRegResult = RegSetValueEx(hCurKey, strValue, 0&, REG_DWORD, lData, 4)
lRegResult = RegCloseKey(hCurKey)
End Sub

Private Sub SaveSettingByte(ByVal hkey As Long, ByVal strPath As String, ByVal strValueName As String, ByData() As Byte)
Dim lRegResult As Long
Dim hCurKey As Long

lRegResult = RegCreateKey(hkey, strPath, hCurKey)
lRegResult = RegSetValueEx(hCurKey, strValueName, 0&, REG_BINARY, ByData(0), UBound(ByData()) + 1)
lRegResult = RegCloseKey(hCurKey)
End Sub