Share via


CreateKey method of the StdRegProv class

The CreateKey method creates a subkey in the specified tree.

This topic uses Managed Object Format (MOF) syntax. For more information about using this method, see Calling a Method.

Syntax

uint32 CreateKey(
  [in] uint32 hDefKey = HKEY_LOCAL_MACHINE,
  [in] String sSubKeyName
);

Parameters

hDefKey [in]

A registry tree, also known as a hive, that contains the sSubKeyName path. The default value is HKEY_LOCAL_MACHINE.

The following trees are defined in WinReg.h.

HKEY_CLASSES_ROOT (2147483648)

HKEY_CURRENT_USER (2147483649)

HKEY_LOCAL_MACHINE (2147483650)

HKEY_USERS (2147483651)

HKEY_CURRENT_CONFIG (2147483653)

sSubKeyName [in]

The key to be created. The CreateKey method creates all subkeys specified in the path that do not exist. For example, if MyKey and MySubKey do not exist in the following path, the CreateKey method creates both keys: HKEY_CURRENT_USER\SOFTWARE\MyKey\MySubKey.

Return value

In C++, the method returns a uint32 value that is 0 (zero) if successful. If the function fails, the return value is a nonzero error code that is defined in WinError.h. In C++, use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the error. You can also look up return values under the WMI Error Constants.

In scripting or Visual Basic, the method returns an integer value that is 0 (zero) if successful. If the function fails, the return value is a nonzero error code that you can look up in WbemErrorEnum.

Remarks

You rarely need to create a registry subkey. However, in some circumstances the solution to a problem - as described in a Knowledge Base article or a security bulletin - requires the addition of a subkey. If you need to create a subkey on a large number of computers, the best solution might very well be a script. After all, a single script can be used to create the subkey on every computer affected by the problem. The Registry Provider's CreateKey method enables you to add a new subkey. It takes two parameters: a constant indicating the subtree to which the new subkey will be added and the path of the new subkey.

Examples

The Add Sites to an Internet Explorer Security Zone VBScript sample adds the Web site Contoso.com to the Trusted sites zone and BenefitsWeb to the Local intranet zone on a computer running Internet Explorer Enhanced Security Configuration.

The following PowerShell and VBScript code examples show how to use the CreateKey method to create the SOFTWARE\NewKey subkeys under HKEY_LOCAL_MACHINE\SOFTWARE.

Const HKEY_CURRENT_USER As Long = &H80000001
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "Software\MyKey\MySubKey"
' Create new key and value
Return = objReg.CreateKey(HKEY_LOCAL_MACHINE, strKeyPath)

If (Return = 0) And (Err.Number = 0) Then    
    Wscript.Echo "HKEY_LOCAL_MACHINE\Software\MyKey\MySubKey created"
Else
    Wscript.Echo "CreateKey failed. Error = " & Err.Number
End If

PowerShell
$HKEY_LOCAL_MACHINE = 2147483650
$strKeyPath = "SOFTWARE\NewKey"

$objReg = [WMIClass]"root\default:StdRegProv" [void]$objReg.CreateKey($HKEY_LOCAL_MACHINE, $strKeyPath)

"Example created at " + $strKeyPath

Requirements

Minimum supported client
Windows Vista
Minimum supported server
Windows Server 2008
Namespace
Root\default
MOF
RegEvent.mof
DLL
Stdprov.dll

See also

StdRegProv

Modifying the System Registry

WMI Tasks: Registry