Visual Basic Source Code - Writing to the Registry For: VB4, VB5, VB6
Visual Basic,
by default using GetSetting and SaveSetting allows reading and writing only to the VB and VBA Programs key under HKEY_Current_User\Software.
There can be any number of reasons to want to read or write other portions of the registry, and that can be accomplished using
API calls. There are two typical ways that this is done; the first is with a Class Module, and the second is with a regular
function or sub. I personally prefer the function/sub approach, which is shown in the code below. My earliest versions of
this code were published on Compuserve in 1996, and have evolved steadily since then.
With this code, you can write
strings to any portion of the registry for which you have permissions. A Read Function is on another page (link at bottom).
First you need a number of declares. Not all of these are used for every registry access, but it will not hurt to include
them in the declarations portion of the .BAS module that contains your function/subroutine. Copy/Paste from the box below: Type
SECURITY_ATTRIBUTES nLength As Long
lpSecurityDescriptor As Long bInheritHandle As Long End Type
Declare Function RegCreateKeyEx& Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey&, ByVal lpSubKey$, ByVal
Reserved&, ByVal lpClass$, ByVal dwOptions&, ByVal samDesired&, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult&,
lpdwDisposition&) Declare Function RegSetValueEx& Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey&, ByVal
lpszValueName$, ByVal dwRes&, ByVal dwType&, lpDataBuff As Any, ByVal nSize&) Declare Function RegCloseKey&
Lib "advapi32.dll" (ByVal hKey&)
Const HKEY_CLASSES_ROOT = &H80000000 Const HKEY_CURRENT_USER = &H80000001 Const HKEY_LOCAL_MACHINE = &H80000002 Const
HKEY_USERS = &H80000003
Const KEY_SET_VALUE = &H2& Const KEY_CREATE_SUB_KEY = &H4& Const REG_SZ = 1& Const REG_DWORD
= 4& Const READ_CONTROL = &H20000 Const STANDARD_RIGHTS_WRITE = READ_CONTROL Const Key_Write = STANDARD_RIGHTS_WRITE
Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY
Now we are ready for the subs themselves. If the value does not exist, it will
be created. This sub will write string (REG_SZ) values:
Sub RegSetValue(H_KEY&, RSubKey$, ValueName$, RegValue$) 'H_KEY must be one of the Key Constants
Dim lRtn& 'returned by registry functions, should be 0&
Dim hKey& 'return handle to opened key Dim lpDisp&
Dim Sec_Att As SECURITY_ATTRIBUTES Sec_Att.nLength = 12& Sec_Att.lpSecurityDescriptor
= 0& Sec_Att.bInheritHandle = False If RegValue = "" Then RegValue = " "
lRtn = RegCreateKeyEx(H_KEY, RSubKey, 0&, "", 0&, Key_Write, Sec_Att,
hKey, lpDisp) If lRtn <> 0 Then
Exit Sub 'No key open, so leave End If
lRtn = RegSetValueEx(hKey, ValueName, 0&, REG_SZ, ByVal RegValue, CLng(Len(RegValue) + 1))
lRtn = RegCloseKey(hKey) End Sub I prefer a separate subroutine to accept numeric values. This sub will write number
(REG_DWORD) values:
Sub RegSetValueNum(H_KEY&, RSubKey$, ValueName$, RegValue&) 'H_KEY must be one of the Key
Constants Dim lRtn& 'returned by registry functions,
should be 0& Dim hKey& 'return handle to opened
key Dim lpDisp& Dim Sec_Att As SECURITY_ATTRIBUTES Sec_Att.nLength
= 12& Sec_Att.lpSecurityDescriptor = 0& Sec_Att.bInheritHandle = False
lRtn = RegCreateKeyEx(H_KEY, RSubKey, 0&, "", 0&, Key_Write, Sec_Att,
hKey, lpDisp) If lRtn <> 0 Then
Exit Sub 'No key open, so leave End If
lRtn = RegSetValueEx(hKey, ValueName, 0&, REG_DWORD, RegValue, 4) lRtn =
RegCloseKey(hKey)
End Sub
|