Yoel,
Been out of action for a while, so only just seen this post. You've had
some good replies on alternative solutions, but in order to use CreateKeyEx
this is a successful implementation I've used:
'-------------------------------------------
' CODE BLOCK
'-------------------------------------------
Dim keyhand As Long
Dim RetVal As Long
Dim Hkey As Long
Dim RegistryPath As String
Dim Name As String
Dim StrValue As String
Dim secSecurity As SECURITY_ATTRIBUTES
Dim KeyExists As Long
Hkey = HKEY_LOCAL_MACHINE ' Which root to use
RegistryPath = "Software\TestKey" ' The path of the registry key
Name = "" ' What setting to save (in this case the default setting)
' create the key and also check if it already exists
RetVal = RegCreateKeyEx(Hkey, RegistryPath, 0, Name, 0, _
KEY_SET_VALUE, secSecurity, keyhand, KeyExists)
' then go on and save a value to the setting
StrValue = "Test Value"
RetVal = RegSetValueEx(keyhand, Name, 0, REG_SZ, _
ByVal StrValue, Len(StrValue))
' close the key
RetVal = RegCloseKey(keyhand)
'-------------------------------------------
' CODE BLOCK ENDS
'-------------------------------------------
From your code that you supplied, I think you've got two problems, you are
attempting to create a new key directly under HKEY_LOCAL_MACHINE which will
fail (try doing it in regedit). Under HKEY_CURRENT_USER this should work, but
there are more restrictions under the other HKEY's. Play in regedit to find
out the limitations.
The other possible problem is that you're passing vbNullString where I would
pass the name of the setting to create. I'm not sure, but that could cause an
additional problem (been a while since I've seriously played with these API
calls).
Hope that helps
Syphtor
Yoel Pedersen wrote:
> Hello all of you,
>
> I have a problem working with the Registry API - I found an article from
> MSDN at http://support.microsoft.com/support/kb/articles/Q145/6/79.asp -
> all the functions and procedures worked when I tested them, just except
> RegCreateKeyEx - what's wrong?
>
> Here are my code declarations:
>
> Option Explicit
>
> Public Const REG_SZ As Long = 1
> Public Const REG_DWORD As Long = 4
>
> Public Const HKEY_CLASSES_ROOT = &H80000000
> Public Const HKEY_CURRENT_USER = &H80000001
> Public Const HKEY_LOCAL_MACHINE = &H80000002
> Public Const HKEY_USERS = &H80000003
>
> Public Const ERROR_NONE = 0
> Public Const ERROR_BADDB = 1
> Public Const ERROR_BADKEY = 2
> Public Const ERROR_CANTOPEN = 3
> Public Const ERROR_CANTREAD = 4
> Public Const ERROR_CANTWRITE = 5
> Public Const ERROR_OUTOFMEMORY = 6
> Public Const ERROR_ARENA_TRASHED = 7
> Public Const ERROR_ACCESS_DENIED = 8
> Public Const ERROR_INVALID_PARAMETERS = 87
> Public Const ERROR_NO_MORE_ITEMS = 259
>
> Public Const KEY_QUERY_VALUE = &H1
> Public Const KEY_SET_VALUE = &H2
> Public Const KEY_ALL_ACCESS = &H3F
>
> Public Const REG_OPTION_NON_VOLATILE = 0
>
> Declare Function RegCloseKey Lib "advapi32.dll" _
> (ByVal hKey As Long) As Long
> 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
> 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
> Declare Function RegQueryValueExString Lib "advapi32.dll" Alias _
> "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
> String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _
> As String, lpcbData As Long) As Long
> Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias _
> "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
> String, ByVal lpReserved As Long, lpType As Long, lpData As _
> Long, lpcbData As Long) As Long
> Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias _
> "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
> String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _
> As Long, lpcbData As Long) As Long
> Declare Function RegSetValueExString Lib "advapi32.dll" Alias _
> "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
> ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As _
> String, ByVal cbData As Long) As Long
> Declare Function RegSetValueExLong Lib "advapi32.dll" Alias _
> "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
> ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, _
> ByVal cbData As Long) As Long
>
> The methods/procedures I used:
>
> Private Function CreateNewKey (sNewKeyName As String, lPredefinedKey As
> Long) As Long
> Dim hNewKey As Long 'handle to the new key
> Dim lRetVal As Long 'result of the RegCreateKeyEx function
>
> lRetVal = RegCreateKeyEx(lPredefinedKey, sNewKeyName, 0&, _
> vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, _
> 0&, hNewKey, lRetVal)
>
> CreateNewKey = lRetVal
> RegCloseKey (hNewKey)
> End Function
>
> I tried this example, but the return code was 87 -
> ERROR_INVALID_PARAMETERS
>
> MsgBox(CreateNewKey "TestKey", HKEY_LOCAL_MACHINE)
>
> What is it I've done wrong?
> Any help is appreciated. Thank you for your time.
>
> Sincerely Yoel Pedersen