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
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.
If successful, the function returns zero. If an error occured, the function returns a non-zero error code.
None.
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
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
RegCloseKey, RegDeleteKey, RegOpenKeyEx
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