SaveSetting Command

Purpose

Saves or creates an application entry in the Windows registry.

Syntax

SaveSetting hkey$, [subkey$], name$, [Int | Bin | Str,] value

Description

Saves a value in the registry under \\HKEY_CURRENT_USER\Software or any other node.

The registry stores data in a hierarchically structured tree. Each node in the tree is called a key. Each key can contain both subkeys and data entries called values.

The SaveSetting command syntax has these arguments:

hkey$ Required. String expression containing the name of the application or project to which the section or key setting applies. The value is saved under "\\hkcu\Software\" + hkey$. May include subkey$.
subkey$ Optional. String expression containing the name of the section where the key setting is stored. By default the value is saved under "\\hkcu\Software\" + hkey$ + "\" + subkey$. hkey$ may include subkey$; subkey$ is then omitted.
name$ Required. String expression containing the name of the key setting to set. If set to "" the default value (Standard) is written.
type Optional. Besides the default type Str, the Int and Bin data types are allowed. In case of Bin, the data must be stored in a string. To save a user-defined type copy the binary data to a string using:
value$ = Peek$(V:udt, SizeOf(udt))
value Expression containing the value that key is being set to.

SaveSetting can also be used to write to other keys than \\hkcu only. When hkey$ starts with "\\" a predefined reserved handle must follow.

"\\HKEY_CLASSES_ROOT" or "\\hkcr" or "\\80000000" (see Known Issues)
"\\HKEY_CURRENT_CONFIG"
"\\HKEY_CURRENT_USER" or "\\hkcu"
"\\HKEY_LOCAL_MACHINE" or "\\hklm"
"\\HKEY_USERS"
"\\HKEY_PERFORMANCE_DATA" (Windows NT)
"\\HKEY_DYN_DATA" (Windows 95 and Windows 98)

The key$ parameter can be assembled using "\\" & Hex(HKEY_CLASSES_ROOT)

When hkey$ starts with "\" it must be followed with a valid key for HKEY_CURRENT_USER, because "\" determines a descendant of hkcu. For instance, SaveSetting "\Software\Company\prog", , "name", "123" writes the same value as SaveSetting "Company", "prog", "name", "123".

Example

1. Save, get, and delete application settings.

SaveSetting "MyApp", "Startup\New", "Top", 75

SaveSetting "MyApp", "Startup\New", "Left", 50

Debug.Print GetSetting("MyApp", "Startup\New", "Left")

MsgBox "Open Registry to verify settings."

DeleteSetting "MyApp", "Startup\New"

DeleteSetting "MyApp", "Startup"

DeleteSetting "MyApp"

2. Create and delete a file association

Dim ext$     = ".zzz"

Dim cmdkey$  = "MyGFA32App.Document"

Dim descr$   = "MyGFA32App Document"

Dim appPath$ = App.FileName & " %1"

SaveSetting "\\HKEY_CURRENT_USER\Software\Classes", ext$, "" cmdkey$

SaveSetting "\\HKEY_CURRENT_USER\Software\Classes", cmdkey$, "", descr$

SaveSetting "\\HKEY_CURRENT_USER\Software\Classes\" + cmdkey$, "shell\open\command", "", appPath$

MsgBox "Open Registry to verify settings."

'Remove File Association

DeleteSetting "\\hkcr", ext$

DeleteSetting "\\hkcr\" + cmdkey$, "shell\open\command"

DeleteSetting "\\hkcr\" + cmdkey$, "shell\open"

DeleteSetting "\\hkcr\" + cmdkey$, "shell"

DeleteSetting "\\hkcr", cmdkey$

Remarks

Note - SaveSetting creates a key when it doesn't exist, even in HKEY_CLASSES_ROOT or "\\hkcr". It is not necessary to use the GFA-BASIC 32 CreateRegKey function to first create the key.

DeleteSetting conforms to the Api function RegDeleteKey(). It removes subkeys only, i.e. you must specify the parent key if you want to remove a key. Always delete keys step by step, because Windows NT does not support removing of nested keys.

Known Issues

In later versions of Windows (certainly from Windows 8 onwards), you can get an error saving a setting directly to \\hkcr or \\HKEY_CURRENT_ROOT if the key does not exist. This is due to changes in Windows security protocols.

To get around this problem, instead of using \\hkcr, use the longer \\HKEY_CURRENT_USER\Software\Classes instead. The latter is actually the source of the former in any one user account and any changes made will be reflected in \\hkcr.

See Also

vbDeleteSetting, vbGetSetting, vbGetSettingType, vbSaveSetting, GetSetting, GetSettingType, DeleteSetting, CreateRegKey, OpenRegKey, CloseRegKey, GetRegVal, GetRegValName, GetRegValType, GetRegValNameCount, GetRegSubKey, GetRegSubKeyCount

{Created by Sjouke Hamstra; Last updated: 22/10/2014 by James Gaite}