RegCreateKeyEx Function

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 SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long

Platforms: Win 32s, Win 95/98, Win NT

RegCreateKeyEx creates a new registry key. If the key you want to create already exists, the existing key will be opened (as if RegOpenKeyEx had been used). The handle to the created/opened key is put into the variable passed as phkResult. The function returns 0 if successful, or a non-zero error code if an error occurs.

hKey
Either a handle to an open registry key or exactly one of the following flags that identify the base key to create the new key under:
HKEY_CURRENT_USER = &H80000001
The HKEY_CURRENT_USER base key, which stores program information for the current user.
HKEY_LOCAL_MACHINE = &H80000002
The HKEY_LOCAL_MACHINE base key, which stores program information for all users.
HKEY_USERS = &H80000003
The HKEY_USERS base key, which has all the information for any user (not just the one provided by HKEY_CURRENT_USER).
HKEY_CURRENT_CONFIG = &H80000005
The HKEY_CURRENT_CONFIG base key, which stores computer configuration information.
HKEY_DYN_DATA = &H80000006
The HKEY_DYN_DATA base key, which stores dynamic data.
lpSubkey
The name of the new key to create.
Reserved
Reserved. Set to 0.
lpClass
The name of the class or object type of the key. You can specify an empty string.
dwOptions
Set to 0 if you want the key to be saved to the registry. Set to 1 if you want the key to be destroyed when Windows shuts down (i.e., do not save).
samDesired
One or more of the following flags specifying the desired read/write access:
KEY_ALL_ACCESS = &HF003F
Permission for all types of access.
KEY_CREATE_LINK = &H20
Permission to create symbolic links.
KEY_CREATE_SUB_KEY = &H4
Permission to create subkeys.
KEY_ENUMERATE_SUB_KEYS = &H8
Permission to enumerate subkeys.
KEY_EXECUTE = &H20019
Same as KEY_READ.
KEY_NOTIFY = &H10
Permission to give change notification.
KEY_QUERY_VALUE = &H1
Permission to query subkey data.
KEY_READ = &H20019
Permission for general read access.
KEY_SET_VALUE = &H2
Permission to set subkey data.
KEY_WRITE = &H20006
Permission for general write access.
lpSecurityAttributes
For Windows NT, the level of security to give the key. For other versions of Windows, set its members to zero.
phkResult
Variable that receives the handle to the new/opened registry key.
lpdwDisposition
Variable that receives 1 if a new key was created, or 2 if an already-existing key was opened.

Example:

' Create a key called HKEY_CURRENT_USER\Software\MyCorp\MyProgram\Config.
' Then create a "username" value under that key and set its value to "Rimmer".
Dim hregkey As Long  ' receives handle to the newly created or opened registry key
Dim secattr As SECURITY_ATTRIBUTES  ' security settings of the key
Dim subkey As String  ' name of the subkey to create
Dim neworused As Long  ' receives 1 if new key was created or 2 if an existing key was opened
Dim stringbuffer As String  ' the string to put into the registry
Dim retval As Long  ' return value

' Set the name of the new key and the default security settings
subkey = "Software\MyCorp\MyProgram\Config"
secattr.nLength = Len(secattr)  ' size of the structure
secattr.lpSecurityDescriptor = 0  ' default security level
secattr.bInheritHandle = True  ' the default value for this setting

' Create or open the registry key
retval = RegCreateKeyEx(HKEY_CURRENT_USER, subkey, 0, "", 0, KEY_WRITE, secattr, hregkey, neworused)
If retval <> 0 Then  ' error during open
  Debug.Print "Error opening or creating registry key -- aborting."
  End  ' terminate the program
End If

' Write the string to the registry.  Note that because Visual Basic is being used, the string passed to the
' function must explicitly be passed ByVal.
stringbuffer = "Rimmer" & vbNullChar  ' note how a null character must be appended to the string
retval = RegSetValueEx(hregkey, "username", 0, REG_SZ, stringbuffer, Len(stringbuffer))  ' write the string

' Close the registry key
retval = RegCloseKey(hregkey)

Related Calls: RegCloseKey, RegDeleteKey, RegOpenKeyEx
Category: Registry
Back to the index.


Go to Paul Kuliniewicz's Home Page.
E-mail: rogue953@hotmail.com
This page is at http://skyscraper.fortunecity.com/transmission/45/api/r/regcreatekeyex.html