Skip to content

CreateKey - cRegistry

Adds a new Key to the Registry

Type: Function
Return Data Type: Integer

Parameters

Parameter Type Description
sKeyName String The name of the Key to create.

Syntax

Function CreateKey String sKeyName Returns Integer

Call Example

Get CreateKey sKeyName to IntegerVariable

Description

CreateKey adds a new Key to the Registry . Attempting to create a key that already exists has no effect and does not produce an error.

When CreateKey creates a Key, it uses the pfAccessRights property to specify its access.

A Key created by CreateKey has no Values. To set the Key's Values, use the WriteBinary, WriteInteger, WriteString and WriteUInt methods.

Example

This example shows how to access the Registry to create a new subkey of the "Data Access Worldwide" key in the HKEY_CURRENT_USER branch and write a string and a dword value to the newly created subkey.

Procedure CreateSubKey String sKey String sSubKey String sTestValue Integer iTestValue
    Boolean bOpened bExists
    Integer iCreated
    Handle hoRegistry

    Get Create (RefClass(cRegistry)) to hoRegistry

    // Set the default of phRootKey...
    Set phRootKey of hoRegistry to HKEY_CURRENT_USER

    // set access rights to read
    Set pfAccessRights of hoRegistry to Key_Read

    Get KeyExists of hoRegistry sKey to bExists
    If bExists Begin
        // Open the Key...
        Get OpenKey of hoRegistry sKey to bOpened
        If bOpened Begin
            // set access rights to create/write new keys and values
            Set pfAccessRights of hoRegistry to KEY_ALL_ACCESS

            // create subkey
            Get CreateKey of hoRegistry (sKey+"\"+sSubKey) to iCreated
            If (iCreated=0) Begin
                // create string and dword values inside new subkey
                Send WriteString of hoRegistry "String Test" sTestValue
                Send WriteInteger of hoRegistry "Integer Test" iTestValue
            End
        End
    End

    Send Destroy of hoRegistry
End_Procedure

Send CreateSubKey "Software\Data Access Worldwide" "My Subkey" "String Test Value" 1234

See Also

OpenKey | pfAccessRights

Return Value

Returns a value of zero if the Key is created successfully. If the Key is not created, the return value will be the error code returned by Windows