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

Description & Usage

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). A handle to the created/opened key is put into the variable passed as phkResult.

Return Value

If successful, the function returns zero. If an error occured, the function returns a non-zero error code.

Visual Basic-Specific Information

None.

Parameters

hKey
Specifies an existing registry under which the new registry key will be created. This is either a handle to an open registry key or exactly one of the following flags that identify a registry base key. (The base key flags are named identically to the base keys they specify.)
HKEY_CLASSES_ROOT
HKEY_CURRENT_CONFIG
HKEY_CURRENT_USER
HKEY_DYN_DATA (Windows 95, 98 only)
HKEY_LOCAL_MACHINE
HKEY_PERFORMANCE_DATA (Windows NT, 2000 only)
HKEY_USERS
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
If this is 0, the key will be saved to the registry. If this is 1, the key will not be permanently saved to the registry, and will disappear when Windows shuts down.
samDesired
One or more of the following flags specifying the desired read/write access:
KEY_ALL_ACCESS
Permission for all types of access.
KEY_CREATE_LINK
Permission to create symbolic links.
KEY_CREATE_SUB_KEY
Permission to create subkeys.
KEY_ENUMERATE_SUB_KEYS
Permission to enumerate subkeys.
KEY_EXECUTE
Same as KEY_READ.
KEY_NOTIFY
Permission to give change notification.
KEY_QUERY_VALUE
Permission to query subkey data.
KEY_READ
Permission for general read access.
KEY_SET_VALUE
Permission to set subkey data.
KEY_WRITE
Permission for general write access.
lpSecurityAttributes
Windows NT, 2000: Specifies the security attributes to assign to the newly created key. Windows 95, 98: Ignored.
phkResult
Receives a handle to the newly created or opened registry key.
lpdwDisposition
Receives 1 if the registry key was newly created, or 2 if an existing key was simply opened.

Constant Definitions

Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_CONFIG = &H80000005
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_DYN_DATA = &H80000006
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_PERFORMANCE_DATA = &H80000004
Const HKEY_USERS = &H80000003
Const KEY_ALL_ACCESS = &HF003F
Const KEY_CREATE_LINK = &H20
Const KEY_CREATE_SUB_KEY = &H4
Const KEY_ENUMERATE_SUB_KEYS = &H8
Const KEY_EXECUTE = &H20019
Const KEY_NOTIFY = &H10
Const KEY_QUERY_VALUE = &H1
Const KEY_READ = &H20019
Const KEY_SET_VALUE = &H2
Const KEY_WRITE = &H20006

Example

This example creates a new registry called "HKEY_CURRENT_USER\Software\MyCorp\MyProgram\Config". Under that key, it creates a "username" value and sets its value to the string "Rimmer". Place a command button named Command1 in a form window to run this example. The example executes when Command1 is clicked.

' This code is licensed according to the terms and conditions listed here.

' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Type SECURITY_ATTRIBUTES
	nLength As Long
	lpSecurityDescriptor As Long
	bInheritHandle As Long
End Type
Public 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
Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal _
	hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType _
	As Long, lpData As Any, ByVal cbData As Long) As Long
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Public Const HKEY_CURRENT_USER = &H80000001
Public Const KEY_WRITE = &H20006
Public Const REG_SZ = 1

' *** Place the following code inside the form. ***

Private Sub Command1_Click()
	Dim hKey As Long            ' receives handle to the registry key
	Dim secattr As SECURITY_ATTRIBUTES  ' security settings for the key
	Dim subkey As String        ' name of the subkey to create or open
	Dim neworused As Long       ' receives flag for if the key was created or 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)
	secattr.lpSecurityDescriptor = 0
	secattr.bInheritHandle = 1
	
	' Create (or open) the registry key.
	retval = RegCreateKeyEx(HKEY_CURRENT_USER, subkey, 0, "", 0, KEY_WRITE, _
		secattr, hKey, neworused)
	If retval <> 0 Then
		Debug.Print "Error opening or creating registry key -- aborting."
		Exit Sub
	End If
	
	' Write the string to the registry.  Note the use of ByVal in the second-to-last
	' parameter because we are passing a string.
	stringbuffer = "Rimmer" & vbNullChar  ' the terminating null is necessary
	retval = RegSetValueEx(hKey, "username", 0, REG_SZ, ByVal stringbuffer, _
		Len(stringbuffer))
	
	' Close the registry key.
	retval = RegCloseKey(hKey)
End Sub

See Also

RegCloseKey, RegDeleteKey, RegOpenKeyEx

Category

Registry

Go back to the Function listing.
Go back to the Reference section index.


Last Modified: July 30, 2000
This page is copyright © 2000 Paul Kuliniewicz. Copyright Information.
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/r/regcreatekeyex.html