Results 1 to 2 of 2

Thread: Existing SubKeys

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2001
    Posts
    19

    Cool Existing SubKeys

    I am using the API function 'RegCreateKeyEx' to write a value to the registry. This function needs a param 'samDesired' and I give it the constant 'REG_CREATEKEY Or KEY_SET_VALUE' where
    REG_CREATEKEY = &H4
    and
    KEY_SET_VALUE = &H2
    This ensures that if the subkey does not exist, it will be created.

    However I need to know if the subkey does not exist and after that decide whether to create it or not.

    I tried to give the function only KEY_SET_VALUE and catch the error, but no error appears, the returnvalue is zero and the (non-existing) subkey is not created, value is not set.

    Can anybody help me with this problem ?

    Thx,
    JHN

  2. #2
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774
    This function will tell you if the key's not there. Creating it at that point is up to you.

    VB Code:
    1. '
    2. 'Usage:
    3. 'blnRetVal = CheckRegistryKey(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\John")
    4. '
    5. ' If blnRetVal = False Then
    6. '      ' create the key here
    7. ' End If
    8. '
    9. '
    10. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
    11.     (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _
    12.     ByVal samDesired As Long, phkResult As Long) As Long
    13.  
    14. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As _
    15.     Long
    16.    
    17. Const KEY_READ = &H20019
    18. Const HKEY_CLASSES_ROOT = &H80000000
    19. Const HKEY_CURRENT_CONFIG = &H80000005
    20. Const HKEY_CURRENT_USER = &H80000001
    21. Const HKEY_LOCAL_MACHINE = &H80000002
    22. Const HKEY_USERS = &H80000003
    23.  
    24. ' Return True if a Registry key exists
    25.  
    26. Public Function CheckRegistryKey(ByVal hKey As KeyValues, ByVal KeyName As String) As Boolean
    27.     Dim handle As Long
    28.     ' Try to open the key
    29.     If RegOpenKeyEx(hKey, KeyName, 0, KEY_READ, handle) = 0 Then
    30.         ' The key exists
    31.         CheckRegistryKey = True
    32.         ' Close it before exiting
    33.         RegCloseKey handle
    34.     End If
    35. End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width