|
-
Sep 25th, 2001, 02:50 AM
#1
Thread Starter
Junior Member
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
-
Sep 26th, 2001, 08:21 PM
#2
Fanatic Member
This function will tell you if the key's not there. Creating it at that point is up to you. 
VB Code:
'
'Usage:
'blnRetVal = CheckRegistryKey(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\John")
'
' If blnRetVal = False Then
' ' create the key here
' End If
'
'
Private 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
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As _
Long
Const KEY_READ = &H20019
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_CONFIG = &H80000005
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
' Return True if a Registry key exists
Public Function CheckRegistryKey(ByVal hKey As KeyValues, ByVal KeyName As String) As Boolean
Dim handle As Long
' Try to open the key
If RegOpenKeyEx(hKey, KeyName, 0, KEY_READ, handle) = 0 Then
' The key exists
CheckRegistryKey = True
' Close it before exiting
RegCloseKey handle
End If
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|