FB_RegSetValue

FB_RegSetValue 1:

The system registry is a hierarchically structured tree. A node in the tree is referred to as a key. Each key may contain subkeys and data values.

The function block "FB_RegSetValue" can be used to write or generate individual key values or new key names and values (subkeys+values) in the branch with the predefined handle HKEY_LOCAL_MACHINE. Any number of value types (e.g. REG_DWORD, REG_SZ) or a maximum of 500 bytes of binary data (REG_BINARY) can be written into the system registry. If a key value does not yet exist, it will automatically be generated.

Comment:

The sSubKey and sValueName strings may not be empty!

FB_RegSetValue 2:

HKEY_LOCAL_MACHINE\SOFTWARE\ for 64 bit operating systems

In a 64 bit Windows operating system all registry entries of and for 32 bit applications are not stored under HKEY_LOCAL_MACHINE\SOFTWARE\ but under HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\.
The function blocks FB_RegQueryValue and FB_RegSetValue work automatically below the WOW6432Node folder like any 32 bit application when a registry entry below the SOFTWARE folder is selected. The redirection is performed automatically by the operating system.

FB_RegSetValue 3: Inputs

VAR_INPUT
    sNetId   : T_AmsNetId;
    sSubKey  : T_MaxString;
    sValName : T_MaxString;
    eValType : E_RegValueType;
    cbData   : UDINT;
    pData    : POINTER TO BYTE;
    bExecute : BOOL;
    tTimeOut : TIME := DEFAULT_ADS_TIMEOUT;
END_VAR

Name

Type

Description

sNetId

T_AmsNetID

A string with the network address of the TwinCAT computer whose system registry is to be read can be specified here. For the local computer an empty string may be specified.

sSubKey

T_MaxString

String with the subkey name

sValName

T_MaxString

String with the value name

eValType

E_RegValueType

The data type format of the registry data to be written, e.g: REG_DWORD or REG_SZ.

cbData

UDINT

The number of value data bytes that are to be read. (In the case of string variables this includes the final null).

pData

BYTE

Address of a data buffer/PLC variable containing the value data. The address can be determined with the ADR operator. The programmer is responsible for dimensioning the data buffer such that cbData data bytes can be read from it.

bExecute

BOOL

The function block is enabled by a positive edge at this input.

tTimeout

TIME 

States the length of the timeout that may not be exceeded by execution of the ADS command.

FB_RegSetValue 4: Outputs

VAR_OUTPUT
    bBusy    : BOOL;
    bError   : BOOL;
    nErrId   : UDINT;
    cbWrite  : UDINT;
END_VAR

Name

Type

Description

bBusy

BOOL

When the function block is enabled, this output is set and remains set until a feedback is received.

bError

BOOL

If an error occurs during the transmission of the command, this output is set after the bBusy output is reset.

nErrId

UDINT

Returns the ADS error number or the command-specific error code (table) when the bError output is set.

Error Codes

Error description

0x00

No error

0x01/

The key with the name sSubKey could not be opened/found.

0x02/

The key value with the name sValName could not be opened/found.

Name

Type

Description

cbWrite

UDINT

Number of successfully written value data bytes.

Example:

In the branch with the predefined handle HKEY_LOCAL_MACHINE a subkey 'SOFTWARE\MyCompany\MyProject' with the key name 'LogFileName', the type REG_SZ

and the value 'c:\MyProject\Log' is to be created and set.

PROGRAM MAIN
VAR
    fbRegSetValue   : FB_RegSetValue;
    bBusy               : BOOL;
    bError          : BOOL;
    nErrId          : UDINT;
    cbWrite         : UDINT;
    bWrite          : BOOL;
    sNewValue       : STRING := 'c:\MyProject\Log';
END_VAR
FB_RegSetValue 5:
FB_RegSetValue 6:

Requirements

Development environment

Target platform

PLC libraries to be integrated (category group)

TwinCAT v3.1.0

PC or CX (WES7/Win7/Win10: TC RT x86/x64, WEC6/7: TC RT x86, WEC7: TC CE7 ARMV7, TC/BSD: TC RT x64, TC OS ARMT2)

Tc2_Utilities (System)