Create Reg Key API

This function will create a new key for your application in HKEY_LOCAL_MACHINE. This has been tested in VB5 / Win98 and worked fine, and requires Win NT 3.1 or Win 95 or later versions of Windows.

Note: I do not accept responsability for messing upi your registry. Make backups frequently if your working with it and make sure you save your data BEFORE running, debugging or compiling any code working with the registry or sub classing.

Calling the function as below will create a key in HKEY_LOCAL_MACHINE\Software called YourAppName with a default string set to "" (blank, that is)

 Call MRJCreateNewKey("Software\YourAppName")

Place the following code in a module!

Option Explicit
'=========
'MRJ Design ©2002 - All Rights Reserved
'http://www.forrentusa.com/mrj/
'Leave the copyright intact for reuse please.
'based on an idea from AllApi.net but with minor corrections to their code.
'=========
'CONSTANT DECLARATIONS
'=========
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003

Public Const REG_OPTION_BACKUP_RESTORE = 4     ' open for backup or restore
Public Const REG_OPTION_VOLATILE = 1           ' Key is not preserved when system is rebooted
Public Const REG_OPTION_NON_VOLATILE = 0       ' Key is preserved when system is rebooted
Public Const STANDARD_RIGHTS_ALL = &H1F0000
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 KEY_CREATE_LINK = &H20
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_NOTIFY = &H10
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_SET_VALUE = &H2
Public Const KEY_READ = ((STANDARD_RIGHTS_READ 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 KEY_EXECUTE = (KEY_READ)
Public 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))

Public Const REG_SZ = 1
Public Const REG_CREATED_NEW_KEY = &H1
Public Const REG_OPENED_EXISTING_KEY = &H2


'=========
FUNCTION/SUB DECLARES
'=========

'FOR REGISTRY SETTINGS
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

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

Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" _
(ByVal hKey As Long, _
ByVal lpSubKey As String) As Long




'=========
'create a new key for your application in HKEY_LOCAL_MACHINE
'=========
Function MRJCreateNewKey(ByVal SubKeyString As String)
   Dim Result As Long 'long returned value by RegOpenKeyEx
   Dim Ret As Long 'long value needed to determin if key was opened or created
   
   'Does the key exist?
   RegOpenKeyEx HKEY_LOCAL_MACHINE, APPLICATION_NAME, 0, KEY_ALL_ACCESS, Result
   'If the key doesn't exist, then create it
   
   If Result = 0 Then
       'Don't forget to specify the Ret value = &H1
       Ret = REG_CREATED_NEW_KEY

   Call RegCreateKeyEx(HKEY_LOCAL_MACHINE, SubKeyString, 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 Function
       End If
   Else
       'not needed but still
       Ret = REG_OPENED_EXISTING_KEY 'this value is = &H2
   End If

'IF you want to delete it on the fly for testing simply uncomment these two lines.
'    Delete the key
'    RegDeleteKey Result, ""

   'close the handle
   RegCloseKey Result
End Function

You might also like...

Comments

Mike J

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.”