Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Editing Registry using VB 1

Status
Not open for further replies.

Bloobird

Programmer
Oct 23, 2001
49
GB
Hi - What is the best way to edit a registry key using VB?

Thanks

Chris
 
Two commands that allow you to edit the registry:

GetSetting
SaveSetting
 
Hi - Thanks for that, but I think that only allows me to edit a small section of the registry (VB and VBA Program Settings under HKEY_CURRENT_USER) - I want to be able to access any part of the registry - is this possible ?
 
Yes it is. You will need to use some API Calls though. Here is some code. In a module place

Public Const SYNCHRONIZE = &H100000
Public Const READ_CONTROL = &H20000
Public Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Public Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
Public Const STANDARD_RIGHTS_ALL = &H1F0000
'-----------------------------------------------------------------------------------------------------------------------
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_NOTIFY = &H10
Public Const KEY_SET_VALUE = &H2
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_READ = ((READ_CONTROL Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Public Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
'-----------------------------------------------------------------------------------------------------------------------
Public Const ERROR_SUCCESS = 0&
'-----------------------------------------------------------------------------------------------------------------------
Public Const REG_SZ = 1
Public Const REG_BINARY = 3
Public Const REG_DWORD = 4
'-----------------------------------------------------------------------------------------------------------------------
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 HKEY_CURRENT_CONFIG = &H80000005
Public Const HKEY_DYN_DATA = &H80000006
'-----------------------------------------------------------------------------------------------------------------------
Public Const REG_CREATED_NEW_KEY = &H1
Public Const REG_OPENED_EXISTING_KEY = &H2
'-----------------------------------------------------------------------------------------------------------------------
' Private:
'
'-----------------------------------------------------------------------------------------------------------------------
' Special Notes:
' Printing Line Length is 120 Characters
'-----------------------------------------------------------------------------------------------------------------------
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
'-----------------------------------------------------------------------------------------------------------------------
Public Declare Function SleepEx Lib "kernel32" (ByVal dwMilliseconds As Long, ByVal bAlertable As Long) As Long
'-----------------------------------------------------------------------------------------------------------------------
Public 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
'-----------------------------------------------------------------------------------------------------------------------
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
'-----------------------------------------------------------------------------------------------------------------------
Public 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
'-----------------------------------------------------------------------------------------------------------------------
Public Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey _
As String, ByVal Reserved As Long, ByVal lpClass As Long, ByVal dwOptions As Long, ByVal samDesired As Long, ByVal _
lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long
'-----------------------------------------------------------------------------------------------------------------------
Public Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As _
String) As Long
'-----------------------------------------------------------------------------------------------------------------------
Public 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 Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, _
ByVal lpName As String, ByVal cbName As Long) As Long
'-----------------------------------------------------------------------------------------------------------------------
Public Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal _
lpValueName As String) As Long
'-----------------------------------------------------------------------------------------------------------------------
Public Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As _
Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, lpData As Any, _
lpcbData As Long) As Long
'-----------------------------------------------------------------------------------------------------------------------



Here is some code examples for reading and writing to the registry. Please note that I just copied and pasted the code and not all the variable definitions.

'---------------------------------------------------------START---------------------------------------------------------
Private Sub VerifySettings()
Dim disposition As Long
Dim sTmp As String

On Error GoTo ErrTrap

sSettings = comSerial.Settings
sPortNum = comSerial.CommPort
sSubKey = "Software\Damage Inc\Com Settings"
If RegOpenKeyEx(lMainKey, sSubKey, 0, KEY_READ, hnd) Then
If RegCreateKeyEx(lMainKey, sSubKey, 0, 0, 0, 0, 0, hnd, disposition) Then
Err.Raise 1001, "VerifySettings() Sub", "Could Not Create Registry Key"
End If
End If

'The Key Has Been Found/or Created, Now Check To See If Previous Settings Are Present

'Check For The Settings Subkey and Retrieve Value If Present, Then Set ComPort 'Settings' Property

sKeyValue = Space$(lLength) 'Pad The sKeyValue Variable
If RegQueryValueEx(hnd, sSettingsKey, 0, REG_SZ, ByVal sKeyValue, lLength) Then '0 Return if Successful
If RegOpenKeyEx(lMainKey, sSubKey, 0, KEY_WRITE, hnd) Then '0 Return if Successful
Err.Raise 1001, "VerifySettings() Sub", "Could Not Open Registry Key"
Else 'The Value Was Not Present, Set To Default Port 'Settings' Property
If RegSetValueEx(hnd, sSettingsKey, 0, REG_SZ, ByVal sSettings, Len(sSettings)) Then
Err.Raise 1001, "VerifySettings() Sub", "Could Not Set Registry Key Settings Value"
End If
End If
Else 'Read Value From Key And Set The Port 'Settings' Property To The Value In The Registry
comSerial.Settings = sKeyValue
End If

'Check For The Port Subkey and Retrieve Value If Present, Then Set ComPort 'Port' Property

sKeyValue = Space$(lLength) 'Pad The sKeyValue Variable
If RegQueryValueEx(hnd, sPortKey, 0, REG_SZ, ByVal sKeyValue, lLength) Then '0 Return if Successful
If RegOpenKeyEx(lMainKey, sSubKey, 0, KEY_WRITE, hnd) Then '0 Return if Successful
Err.Raise 1001, "VerifySettings() Sub", "Could Not Open Registry Key"
Else 'The Value Was Not Present, Set To Default Port 'Port' Property
If RegSetValueEx(hnd, sPortKey, 0, REG_SZ, ByVal sPortNum, Len(sPortNum)) Then
Err.Raise 1001, "VerifySettings() Sub", "Could Not Set Registry Key Port Value"
End If
End If
Else 'Read Value From Key And Set The Port 'Port' Property To The Value In The Registry
comSerial.CommPort = sKeyValue
End If

RegCloseKey hnd
Exit Sub

ErrTrap:
MsgBox Err.Number & " " & Err.Description & vbCr & " Error Generated By " & Err.Source, vbCritical, _
"System Error Trap !"
End Sub
'----------------------------------------------------------END----------------------------------------------------------
'Changes The Registry Entries When The User Changes Port Settings
'---------------------------------------------------------START---------------------------------------------------------
Private Sub UpdateSettings()

On Error GoTo ErrTrap

sSettings = comSerial.Settings
sPortNum = comSerial.CommPort
sSubKey = "Software\Damage Inc\Com Settings"

If RegOpenKeyEx(lMainKey, sSubKey, 0, KEY_WRITE, hnd) Then '0 Return if Successful
Err.Raise 1001, "VerifySettings() Sub", "Could Not Open Registry Key"
Else 'The Value Was Not Present, Set To Default Port 'Settings' Property
If RegSetValueEx(hnd, sSettingsKey, 0, REG_SZ, ByVal sSettings, Len(sSettings)) Then
Err.Raise 1001, "VerifySettings() Sub", "Could Not Set Registry Key Settings Value"
End If
End If

If RegOpenKeyEx(lMainKey, sSubKey, 0, KEY_WRITE, hnd) Then '0 Return if Successful
Err.Raise 1001, "VerifySettings() Sub", "Could Not Open Registry Key"
Else 'The Value Was Not Present, Set To Default Port 'Port' Property
If RegSetValueEx(hnd, sPortKey, 0, REG_SZ, ByVal sPortNum, Len(sPortNum)) Then
Err.Raise 1001, "VerifySettings() Sub", "Could Not Set Registry Key Port Value"
End If
End If

Exit Sub

ErrTrap:
MsgBox Err.Number & " " & Err.Description & vbCr & " Error Generated By " & Err.Source, vbCritical, _
"System Error Trap !"
End Sub
'----------------------------------------------------------END----------------------------------------------------------

Hope this at least gets you started. Anything is possible, the problem is I only have one lifetime.
[cheers]
 
Looks good to me - I'll have a play around with it - Thanks !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top