RegCreateKeyTransactedA function (winreg.h)
Creates the specified registry key and associates it with a transaction. If the key already exists, the function opens it. Note that key names are not case sensitive.
Applications that back up or restore system state including system files and registry hives should use the Volume Shadow Copy Service instead of the registry functions.
Syntax
LSTATUS RegCreateKeyTransactedA(
[in] HKEY hKey,
[in] LPCSTR lpSubKey,
DWORD Reserved,
[in, optional] LPSTR lpClass,
[in] DWORD dwOptions,
[in] REGSAM samDesired,
[in, optional] const LPSECURITY_ATTRIBUTES lpSecurityAttributes,
[out] PHKEY phkResult,
[out, optional] LPDWORD lpdwDisposition,
[in] HANDLE hTransaction,
PVOID pExtendedParemeter
);
Parameters
[in] hKey
A handle to an open registry key. The calling process must have KEY_CREATE_SUB_KEY access to the key. For more information, see Registry Key Security and Access Rights.
Access for key creation is checked against the security descriptor of the registry key, not the access mask specified when the handle was obtained. Therefore, even if hKey was opened with a samDesired of KEY_READ, it can be used in operations that create keys if allowed by its security descriptor.
This handle is returned by the RegCreateKeyTransacted or RegOpenKeyTransacted function, or it can be one of the following predefined keys:
- HKEY_CLASSES_ROOT
- HKEY_CURRENT_CONFIG
- HKEY_CURRENT_USER
- HKEY_LOCAL_MACHINE
- HKEY_USERS
[in] lpSubKey
The name of a subkey that this function opens or creates. The subkey specified must be a subkey of the key identified by the hKey parameter; it can be up to 32 levels deep in the registry tree. For more information on key names, see Structure of the Registry.
If lpSubKey is a pointer to an empty string, phkResult receives a new handle to the key specified by hKey.
This parameter cannot be NULL.
Reserved
This parameter is reserved and must be zero.
[in, optional] lpClass
The user-defined class of this key. This parameter may be ignored. This parameter can be NULL.
[in] dwOptions
This parameter can be one of the following values.
Value | Meaning |
---|---|
|
If this flag is set, the function ignores the samDesired parameter and attempts to open the key with the access required to backup or restore the key. If the calling thread has the SE_BACKUP_NAME privilege enabled, the key is opened with the ACCESS_SYSTEM_SECURITY and KEY_READ access rights. If the calling thread has the SE_RESTORE_NAME privilege enabled, the key is opened with the ACCESS_SYSTEM_SECURITY and KEY_WRITE access rights. If both privileges are enabled, the key has the combined access rights for both privileges. For more information, see Running with Special Privileges. |
|
This key is not volatile; this is the default. The information is stored in a file and is preserved when the system is restarted. The RegSaveKey function saves keys that are not volatile. |
|
All keys created by the function are volatile. The information is stored in memory and is not preserved when the corresponding registry hive is unloaded. For HKEY_LOCAL_MACHINE, this occurs when the system is shut down. For registry keys loaded by the RegLoadKey function, this occurs when the corresponding RegUnLoadKey is performed. The RegSaveKey function does not save volatile keys. This flag is ignored for keys that already exist. |
[in] samDesired
A mask that specifies the access rights for the key to be created. For more information, see Registry Key Security and Access Rights.
[in, optional] lpSecurityAttributes
A pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpSecurityAttributes is NULL, the handle cannot be inherited.
The lpSecurityDescriptor member of the structure specifies a security descriptor for the new key. If lpSecurityAttributes is NULL, the key gets a default security descriptor. The ACLs in a default security descriptor for a key are inherited from its direct parent key.
[out] phkResult
A pointer to a variable that receives a handle to the opened or created key. If the key is not one of the predefined registry keys, call the RegCloseKey function after you have finished using the handle.
[out, optional] lpdwDisposition
A pointer to a variable that receives one of the following disposition values.
Value | Meaning |
---|---|
|
The key did not exist and was created. |
|
The key existed and was simply opened without being changed. |
If lpdwDisposition is NULL, no disposition information is returned.
[in] hTransaction
A handle to an active transaction. This handle is returned by the CreateTransaction function.
pExtendedParemeter
This parameter is reserved and must be NULL.
Return value
If the function succeeds, the return value is ERROR_SUCCESS.
If the function fails, the return value is a nonzero error code defined in Winerror.h. You can use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the error.
Remarks
When a key is created using this function, subsequent operations on the key are transacted. If a non-transacted operation is performed on the key before the transaction is committed, the transaction is rolled back. After a transaction is committed or rolled back, you must re-open the key using RegCreateKeyTransacted or RegOpenKeyTransacted with an active transaction handle to make additional operations transacted. For more information about transactions, see Kernel Transaction Manager.
Note that subsequent operations on subkeys of this key are not automatically transacted. Therefore, RegDeleteKeyEx does not perform a transacted delete operation. Instead, use the RegDeleteKeyTransacted function to perform a transacted delete operation.
The key that the RegCreateKeyTransacted function creates has no values. An application can use the RegSetValueEx function to set key values.
The RegCreateKeyTransacted function creates all missing keys in the specified path. An application can take advantage of this behavior to create several keys at once. For example, an application can create a subkey four levels deep at the same time as the three preceding subkeys by specifying a string of the following form for the lpSubKey parameter:
subkey1\subkey2\subkey3\subkey4
Note that this behavior will result in creation of unwanted keys if an existing key in the path is spelled incorrectly.
An application cannot create a key that is a direct child of HKEY_USERS or HKEY_LOCAL_MACHINE. An application can create subkeys in lower levels of the HKEY_USERS or HKEY_LOCAL_MACHINE trees.
Note
The winreg.h header defines RegCreateKeyTransacted as an alias which automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. Mixing usage of the encoding-neutral alias with code that not encoding-neutral can lead to mismatches that result in compilation or runtime errors. For more information, see Conventions for Function Prototypes.
Requirements
Requirement | Value |
---|---|
Minimum supported client | Windows Vista [desktop apps only] |
Minimum supported server | Windows Server 2008 [desktop apps only] |
Target Platform | Windows |
Header | winreg.h (include Windows.h) |
Library | Advapi32.lib |
DLL | Advapi32.dll |