RegCreateKeyEx function
Creates the specified registry key. If the key already exists, the function opens it. Note that key names are not case sensitive.
To perform transacted registry operations on a key, call the RegCreateKeyTransacted function.
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
LONG WINAPI RegCreateKeyEx( __in HKEY hKey, __in LPCTSTR lpSubKey, __reserved DWORD Reserved, __in_opt LPTSTR lpClass, __in DWORD dwOptions, __in REGSAM samDesired, __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes, __out PHKEY phkResult, __out_opt LPDWORD lpdwDisposition );
Parameters
- hKey [in]
-
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 modify the registry if allowed by its security descriptor.
This handle is returned by the RegCreateKeyEx or RegOpenKeyEx 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
- lpSubKey [in]
-
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.
- lpClass [in, optional]
-
The user-defined class type of this key. This parameter may be ignored. This parameter can be NULL.
- dwOptions [in]
-
This parameter can be one of the following values.
Value Meaning - REG_OPTION_BACKUP_RESTORE
- 0x00000004L
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, beginning with Windows Vista, the key is opened with the ACCESS_SYSTEM_SECURITY, DELETE 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.
- REG_OPTION_CREATE_LINK
- 0x00000002L
Note Registry symbolic links should only be used for for application compatibility when absolutely necessary.
This key is a symbolic link. The target path is assigned to the L"SymbolicLinkValue" value of the key. The target path must be an absolute registry path.
- REG_OPTION_NON_VOLATILE
- 0x00000000L
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.
- REG_OPTION_VOLATILE
- 0x00000001L
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.
- samDesired [in]
-
A mask that specifies the access rights for the key to be created. For more information, see Registry Key Security and Access Rights.
- lpSecurityAttributes [in, optional]
-
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.
- phkResult [out]
-
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.
- lpdwDisposition [out, optional]
-
A pointer to a variable that receives one of the following disposition values.
Value Meaning - REG_CREATED_NEW_KEY
- 0x00000001L
The key did not exist and was created.
- REG_OPENED_EXISTING_KEY
- 0x00000002L
The key existed and was simply opened without being changed.
If lpdwDisposition is NULL, no disposition information is returned.
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
The key that the RegCreateKeyEx function creates has no values. An application can use the RegSetValueEx function to set key values.
The RegCreateKeyEx 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.
If your service or application impersonates different users, do not use this function with HKEY_CURRENT_USER. Instead, call the RegOpenCurrentUser function.
Note that operations that access certain registry keys are redirected. For more information, see Registry Virtualization and 32-bit and 64-bit Application Data in the Registry.
Requirements
|
Minimum supported client | Windows 2000 Professional |
|---|---|
|
Minimum supported server | Windows 2000 Server |
|
Header |
|
|
Library |
|
|
DLL |
|
|
Unicode and ANSI names | RegCreateKeyExW (Unicode) and RegCreateKeyExA (ANSI) |
See also
- RegCloseKey
- RegDeleteKey
- Registry Functions
- Registry Overview
- RegOpenKeyEx
- RegSaveKey
- SECURITY_ATTRIBUTES
Send comments about this topic to Microsoft
Build date: 9/7/2011
>> I think we cant embed UNICODE in to registry,can we???any replies?
>> reg add "HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.1
>> \MSSearch\Filters\.ŮEiYkITcŁQčžoRiBZiěĂŠjuEŐBŠEůó6mnRDŢúďÁjřSĽCS <etc utf8>
@saisatish ... Stanley Roark:
The sample you have given contains UTF8 code from a command prompt. A command prompt is ANSI and as such interpreted. Unicode in windows is only standard as UTF16 and the registyr DOES support Unicode (as UTF16) but you need to call the registry calls directly instead from a commando prompt
If aren't able to write an application using the calls then write the keys to a *.reg file version 5.0 in UTF16 format, then call regedit to import the settings. A *.reg file also supports deleting keys by prepending a dash (-) in front of a key or in front of a value.
- 9/5/2010
- ArnoudMulder
- 9/5/2010
- ArnoudMulder
solution:
// ensure 16bit aligned string if unicode
#ifdef UNICODE
in_KeyPath = EnsureAlignedString(in_KeyPath, lv_PathBuf, 256);
#endif
// open subkey
if (RegOpenKeyEx(in_hBaseKey, in_KeyPath, 0, KEY_QUERY_VALUE, &lv_hKey) != 0)
return FALSE;
where:
TCHAR *EnsureAlignedString(TCHAR *in_Str, TCHAR *in_Buf, INT in_MaxLen)
{
// registry calls needs unicode strings to be aligned on 16bit boundaries
if ((INT)in_Str & 1)
{
// alignment is needed, copy to aligned buffer
tstrncpyeos(in_Buf, in_Str, in_MaxLen-1);
// use the buffer
return in_Buf;
}
else
{
// already aligned
return in_Str;
}
}
- 4/20/2010
- ArnoudMulder
I think we can't embed UNICODE in to registry. Can we???
reg add "HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.1\MSSearch\Filters\.ŮEiYkITcŁQčžoRiBZiěĂŠjuEŐBŠEůó6mnRDŢúďÁjřSĽCSkxDI6iguAXëiRIoKb‘mAoLN0Ťpĺü2IqżuŮMHuúlrOLG5muQHÓUJHCMNThčwZC|7RXFlKSdé8Ř0lÓFWBJOy5Ţf4ž9mŘc9UNµCqâGŘűÚgáRDs3PPŇ9goůMtoyIn9łU5IOR1č1IOŘhHÖCżZyÎlűYŰ0aŹoPILigV0guţlTężH6VşIůHŻäiZI06łhPűsfijőÖoUüióTű3ŘSeŁ8zuiiYżFdš" /ve /d {C7310720-AC80-11D1-8DF3-00C04FB6EF4F} /f
- 10/30/2007
- saisatish
- 1/25/2009
- Stanley Roark
http192I don't know how the function behaves with long lpSubKey values and I do not have the time to test it now, but according to http://msdn2.microsoft.com/en-us/library/ms724844.aspx the maximum size of a key name is 255 characters, although I could create keys with names of 256 characters with command line reg tool:
reg add "HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.1\MSSearch\Filters\.ŮEiYkITcŁQčžoRiBZiěĂŠjuEŐBŠEůó6mnRDŢúďÁjřSĽCSkxDI6iguAXëiRIoKb‘mAoLN0Ťpĺü2IqżuŮMHuúlrOLG5muQHÓUJHCMNThčwZC|7RXFlKSdé8Ř0lÓFWBJOy5Ţf4ž9mŘc9UNµCqâGŘűÚgáRDs3PPŇ9goůMtoyIn9łU5IOR1č1IOŘhHÖCżZyÎlűYŰ0aŹoPILigV0guţlTężH6VşIůHŻäiZI06łhPűsfijőÖoUüióTű3ŘSeŁ8zuiiYżFdš" /ve /d {C7310720-AC80-11D1-8DF3-00C04FB6EF4F} /f
RegEdit had a limit somewhere lower than 250 characters.
- 5/29/2007
- xyzzer_at_ms
- 11/23/2008
- Thomas Lee
- 11/29/2007
- Gideon7
