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_CURRENT_CONFIG = &H80000005
Public Const HKEY_PERFORMANCE_DATA = 0
Public Const HKEY_PERFORMANCE_TEXT = 0 ' after Windows 2000, Unicode
Public Const HKEY_PERFORMANCE_NLSTEXT = 0 ' after Windows 2000, Unicode
Public Const HKEY_CURRENT_USER_LOCAL_SETTINGS = 0 ' as of Windows 7 and Windows Server 2008
Public Enum RegistrySections
HK_Root = HKEY_CLASSES_ROOT
HK_Current_User = HKEY_CURRENT_USER
HK_Local_Machine = HKEY_LOCAL_MACHINE
HK_Users = HKEY_USERS
HK_Current_Config = HKEY_CURRENT_CONFIG
End Enum
Public Const REG_NONE = 0&
Public Const REG_SZ = 1& ' Unicode nul terminated string
Public Const REG_EXPAND_SZ = 2&
Public Const REG_BINARY = 3&
Public Const REG_DWORD = 4& ' 32-bit number
Public Const REG_DWORD_LITTLE_ENDIAN = 4&
Public Const REG_DWORD_BIG_ENDIAN = 5&
Public Const REG_LINK = 6&
Public Const REG_MULTI_SZ = 7& '? multi-line string and foreign text?
Public Const REG_RESOURCE_LIST = 8&
Public Const REG_FULL_RESOURCE_DESCRIPTOR = 9&
Public Const REG_RESOURCE_REQUIREMENTS_LIST = 10&
Public Const REG_QWORD = 11& ' 64-bit number
Public Const REG_QWORD_LITTLE_ENDIAN = 11&
Public Enum RegistryDataTypes
TypeUndefined = REG_NONE
TypeString = REG_SZ ' null-terminated string
TypeExpandedString = REG_EXPAND_SZ ' null-terminated string
TypeBinary = REG_BINARY
Type32Bit_Number = REG_DWORD
TypeSymbolicLink = REG_LINK
TypeMultipleStrings = REG_MULTI_SZ ' double-null-terminated string
TypeResourceList = REG_RESOURCE_LIST
TypeResourceRequirements = REG_RESOURCE_REQUIREMENTS_LIST
Type64Bit_Number = REG_QWORD
End Enum
Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
(ByVal hRegistryKey As Long, _
ByVal lpValueName As String, _
ByVal dwReserved As Long, _
ByVal dwDataType As Long, _
ByVal lpDataBuffer As Any, _
ByVal cbDataSize As Long) _
As Long
...
Sub X
...
RegistrySetValue RegistrySections.HK_Current_User, strAccountKey, _
cLeaveOnServer, &H10003, RegistryDataTypes.Type32Bit_Number
End Sub
...
'hTopKey must be one of the Key Constants from RegistrySection Enum
Dim lErrCode As Long 'returned by registry functions, should be 0&
Dim RegistryKey As RegistryDataTypes 'return handle to opened key
Dim lpDisposition As Long '?
Dim Security_Attr As LPSECURITY_ATTRIBUTES
On Error GoTo HandleErr
Security_Attr.nLength = Len(Security_Attr) ' 12&
Security_Attr.lpSecurityDescriptor = 0&
Security_Attr.bInheritHandle = CLng(True) ' False
lErrCode = RegCreateKeyEx(hTopKey, RegistryPath, 0&, vbNullString, REG_OPTION_NON_VOLATILE, KEY_WRITE, Security_Attr, RegistryKey, lpDisposition)
If lErrCode <> ERROR_SUCCESS Then
Exit Sub 'No key open, so leave
End If
If DataType = Type32Bit_Number Then
lErrCode = RegSetValueEx(RegistryKey, RegistryName, 0&, DataType, ByVal CLng(Val(RegistryValue)), 4)
Else
lErrCode = RegSetValueEx(RegistryKey, RegistryName, 0&, DataType, ByVal RegistryValue, CLng(Len(RegistryValue) + 1))
End If
lErrCode = RegCloseKey(RegistryKey)
ExitHere:
Exit Sub
HandleErr:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Registry Functions.RegistrySetValue"
End Select
Resume ExitHere
' End Error handling block.
End Sub
Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.
”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.
Our community of experts have been thoroughly vetted for their expertise and industry experience.
The Most Valuable Expert award recognizes technology experts who passionately share their knowledge with the community, demonstrate the core values of this platform, and go the extra mile in all aspects of their contributions. This award is based off of nominations by EE users and experts. Multiple MVEs may be awarded each year.
The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.