RegCreateKeyEx

来源:互联网 发布:美萌家cos 淘宝店铺 编辑:程序博客网 时间:2024/04/17 00:47

The RegCreateKeyEx function creates the specified registry key. If the key already exists, the function opens it.

LONG RegCreateKeyEx(  HKEY hKey,                                  // handle to open key  LPCTSTR lpSubKey,                           // subkey name  DWORD Reserved,                             // reserved  LPTSTR lpClass,                             // class string  DWORD dwOptions,                            // special options  REGSAM samDesired,                          // desired security access  LPSECURITY_ATTRIBUTES lpSecurityAttributes, // inheritance  PHKEY phkResult,                            // key handle   LPDWORD lpdwDisposition                     // disposition value buffer);

Parameters

hKey
[in] Handle to a currently open key or one of the following predefined reserved handle values:

HKEY_CLASSES_ROOT
HKEY_CURRENT_CONFIG
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS
Windows NT/2000: HKEY_PERFORMANCE_DATA
Windows 95/98: HKEY_DYN_DATA

The key opened or created by the RegCreateKeyEx function is a subkey of the key identified by the hKey parameter.

lpSubKey
[in] Pointer to a null-terminated string specifying 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. This parameter cannot be NULL.

Windows NT/2000: The subkey name specified by lpSubKey must not begin with the backslash character ('/'). If it does, ERROR_BAD_PATHNAME is returned.

Windows 95/98: Beginning backslash characters in the subkey name specified by lpSubKey are ignored.

Reserved
Reserved; must be zero.
lpClass
[in] Pointer to a null-terminated string that specifies the class (object type) of this key. This parameter is ignored if the key already exists. No classes are currently defined; applications should pass a null string. Windows 95 and Windows 98 use this parameter only for remote registry keys; it is ignored for local registry keys. Windows NT/Windows 2000 supports this parameter for both local and remote registry keys.
dwOptions
[in] Specifies special options for the key. This parameter can be one of the following values. ValueMeaningREG_OPTION_NON_VOLATILEThis 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_VOLATILEWindows NT/2000: 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.

Windows 95: This value is ignored. If REG_OPTION_VOLATILE is specified, the RegCreateKeyEx function creates nonvolatile keys and returns ERROR_SUCCESS.

REG_OPTION_BACKUP_RESTORE Windows NT/2000: 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 ACCESS_SYSTEM_SECURITY and KEY_READ access. If the calling thread has the SE_RESTORE_NAME privilege enabled, the key is opened with ACCESS_SYSTEM_SECURITY and KEY_WRITE access. If both privileges are enabled, the key has the combined accesses for both privileges.
samDesired
[in] An access mask that specifies the desired access rights to the key. This parameter can be a combination of the following values. ValueMeaningKEY_CREATE_LINKPermission to create a symbolic link.KEY_CREATE_SUB_KEYPermission to create subkeys.KEY_ENUMERATE_SUB_KEYSPermission to enumerate subkeys.KEY_EXECUTEPermission for read access.KEY_NOTIFYPermission for change notification.KEY_QUERY_VALUEPermission to query subkey data.KEY_SET_VALUEPermission to set subkey data.KEY_ALL_ACCESSCombines the KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS, KEY_NOTIFY, KEY_CREATE_SUB_KEY, KEY_CREATE_LINK, and KEY_SET_VALUE access rights, plus all the standard access rights except SYNCHRONIZE. KEY_READCombines the STANDARD_RIGHTS_READ, KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS, and KEY_NOTIFY access rights.KEY_WRITECombines the STANDARD_RIGHTS_WRITE, KEY_SET_VALUE, and KEY_CREATE_SUB_KEY access rights.
lpSecurityAttributes
[in] 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.

Windows NT/2000: 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.

phkResult
[out] Pointer to a variable that receives a handle to the opened or created key. When you no longer need the returned handle, call the RegCloseKey function to close it.
lpdwDisposition
[out] Pointer to a variable that receives one of the following disposition values. ValueMeaningREG_CREATED_NEW_KEYThe key did not exist and was created.REG_OPENED_EXISTING_KEYThe key existed and was simply opened without being changed.

If lpdwDisposition is NULL, no disposition information is returned.

Return Values

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 RegSetValue or RegSetValueEx function to set key values.

The key identified by the hKey parameter must have been opened with KEY_CREATE_SUB_KEY access. To open the key, use the RegCreateKeyEx or RegOpenKeyEx function.

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.

An application can use RegCreateKeyEx to temporarily lock a portion of the registry. When the locking process creates a new key, it receives the disposition value REG_CREATED_NEW_KEY, indicating that it "owns" the lock. Another process attempting to create the same key receives the disposition value REG_OPENED_EXISTING_KEY, indicating that another process already owns the lock.

Windows 95/98: No registry subkey or value name may exceed 255 characters.

原创粉丝点击