'~~> Constants for the Registry Hive in case you want to experiment
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
'~~> Other Constants for RegCreateKeyEx
Const REG_OPTION_BACKUP_RESTORE = 4 '<~~ open for backup or restore
Const REG_OPTION_VOLATILE = 1 '<~~ Key isn't preserved if system is rebooted
Const REG_OPTION_NON_VOLATILE = 0 '<~~ Key is preserved if system is rebooted
Const STANDARD_RIGHTS_ALL = &H1F0000
Const SYNCHRONIZE = &H100000
Const READ_CONTROL = &H20000
Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
Const KEY_CREATE_LINK = &H20
Const KEY_CREATE_SUB_KEY = &H4
Const KEY_ENUMERATE_SUB_KEYS = &H8
Const KEY_NOTIFY = &H10
Const KEY_QUERY_VALUE = &H1
Const KEY_SET_VALUE = &H2
Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or _
KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or _
KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
Const KEY_EXECUTE = (KEY_READ)
Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or _
KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or _
KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
'~~> The RegCloseKey function releases the handle of the specified key.
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
'~~> The RegDeleteKey function deletes the specified key
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" _
(ByVal hKey As Long, ByVal lpSubKey As String) As Long
'~~> The RegCreateKeyEx function creates the specified key. If the key already exists
'in the registry, the function opens it.
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, _
lpSecurityAttributes As Any, phkResult As Long, lpdwDisposition As Long) As Long
'~~> The RegOpenKeyEx function opens the specified key.
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
(ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal _
samDesired As Long, phkResult As Long) As Long
Private Sub Command1_Click()
'~~> Code adapted from APIGuide <~~'
Dim Result As Long
'~~> Check if the specified key "koolsid" exists under HKEY_CLASSES_ROOT
RegOpenKeyEx HKEY_CLASSES_ROOT, "koolsid", 0, KEY_ALL_ACCESS, Result
'~~> If the key doesn't exist, we create it
If Result = 0 Then
'~~> Create a new key under HKEY_CLASSES_ROOT
RegCreateKeyEx HKEY_CLASSES_ROOT, "koolsid", 0, "REG_DWORD", _
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, Result, ret
If Result = 0 Then
MsgBox "Error while creating the Key!!"
Exit Sub
End If
'~~> Inform user that the key has been created
MsgBox "Key Created..."
End If
'~~> Inform user before deleting the key
MsgBox "Deleting the Key..."
'~~> Delete the key
RegDeleteKey Result, ""
'~~> Close the handle
RegCloseKey Result
End Sub