Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Visual Basic >> Snippet

Edit registry key


'VB GetSetting/SaveSetting is to HKEY_CURRENT_USER\Software\VB and VBA\Program Settings
'so to edit Registry keys in general, use the API

Private Declare Function RegCloseKey Lib "advapi32.dll" ( ByVal hKey As Long ) As Long
Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" ( ByVal hKey As Long , _
   ByVal lpSubKey As String , ByVal Reserved As Long ,   ByVal lpClass As String , ByVal dwOptions As Long , _
   ByVal samDesired As Long , ByVal lpSecurityAttributes As Long , phkResult As Long , lpdwDisposition As Long ) As Long

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


'KeyName: Name of key to create
'ParentKey: Top level key under which the new key will be created. Examples:
'HKEY_CURRENT_USER
'HKEY_LOCAL_MACHINE/Software/MyApp/Settings
'returns True if executes without error, false otherwise

Public Function CreateKey(KeyName As String , ParentKey As Long ) As Boolean
Dim iNewKeyHnd As Long   
Dim iAns As Long         
iAns = RegCreateKeyEx(ParentKey, KeyName, 0&, vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0&, iNewKeyHnd, iAns)
RegCloseKey iNewKeyHnd
CreateKey = (lAns = 0)
End Function