GetSetting, GetSettingType Function

Purpose

Returns a key setting value or type from an entry in the Windows registry.

Syntax

$ = GetSetting(hkey$, [subkey$], [name$] [, Str | Int | Bin] [,default])

% = GetSettingType(hkey$, [subkey$], [name$])

Description

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 GetSetting function 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$ Optional. String expression containing the name of the key setting to return. If omitted the default value (Standard) is returned.
type Optional. Besides the default type Str, the Int and Bin data types are allowed. In case of Bin, the data is returned in a string. To restore the data to a user-defined type copy the string data to the udt using:
Poke$ V: udt, value$
default Optional. Expression containing the value to return if no value is set in the key setting.

When an integer value is read as a Str, the integer is converted using Dec$, internally. Reading an integer as Bin will convert the 4 bytes to a string using Mkl$(). When a Str is read as an Int, Val() is applied. When a Bin value is read as an integer, only the first 4 bytes ar read as numeric value. Str and Bin are equivalent.

In case of an error, the default value is returned. If omitted, default is assumed to be a zero-length string ("") or 0.

GetSetting can also be used to read other keys than \\hkcu\Software only.

Print GetSetting("\\hkcr\.g32", , "")

Returns value for the "Standard" entry: "G32File"

When hkey$ starts with "\\" a predefined reserved handle must follow:

"\\HKEY_CLASSES_ROOT" or "\\hkcr" or "\\80000000"
"\\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 hkey$ parameter can be assembled using "\\" & Hex(HKEY_CLASSES_ROOT)

Print GetSetting("\\" & Hex(HKEY_CLASSES_ROOT) & "\.g32", , "")

When hkey$ starts with "\" it must be followed with a valid key for HKEY_CURRENT_USER, because "\" determines a descendant of hkcu. For instance, the following statements return the same value

a$ = GetSetting("\Software\Firma\prog", , "name")

a$ = GetSetting("Firma", "prog", "name")

The hkey$ parameter may also specify the handle to a registry key obtained using OpenRegKey, see example.

GetSettingType returns the data type for the registry value. The returns value is 1 (REG_SZ) for a string, 3 (REG_BINARY) for binary data, 4 (REG_DWORD) for an Int- value, or 0 (REG_NONE) in case of an error.

Print GetSettingType("\\hkcr\.g32", , "") // returns the Standard name data type: 1 (REG_SZ)

Example

PrintWrap = 1

Local hkey$, value$, i%, t#

// this selects all values in the key and returns first the time

// for the access with OpenRegKey, after without

Local key$ = "\\HKEY_LOCAL_MACHINE\Software" _

"\Microsoft\Windows\CurrentVersion"

If IsWinNT     // GetVersion() > 0

key$ = "\\HKEY_LOCAL_MACHINE\Software" _

"\Microsoft\Windows NT\CurrentVersion"

End If

Print "OpenRegKey + GetSetting"

t = Timer

Restore

hkey$ = OpenRegKey(key$)

For i% = 1 To 50

Read value$

Exit If value$ = "@"

Write GetSetting(hkey$, , value$);

Print ", ";

Next

~CloseRegKey(hkey$)

Print : Print Timer - t

Print "GetSetting only"

t = Timer

Restore

hkey$ = key$

For i% = 1 To 50

Read value$

Exit If value$ = "@"

Write GetSetting(hkey$, , value$);

Print ", ";

Next

Print : Print Timer - t

Print "OpenRegKey + GetRegValCount + GetRegVal"

t = Timer

hkey$ = OpenRegKey(key$)

For i% = 1 To GetRegValCount(hkey$)

Write GetRegVal(hkey$, i);

Print ", ";

Next

CloseRegKey hkey$

Print : Print Timer - t

// This is a list of the value names for the Registry

// directory on a Windows 98 computer.

Data InstallType,SetupFlags,DevicePath, _

ProductType,RegisteredOwner

Data RegisteredOrganization,ProductId, _

LicensingInfo,DVD_Region,BPC_Region

Data OldWinVer,SubVersionNumber, _

ProgramFilesDir,CommonFilesDir,WallPaperDir

Data MediaPath,ConfigPath,SystemRoot, _

OldWinDir,ProductName,Registration _ ExtDLL

Data RegDone,FirstInstallDateTime,Version, _

VersionNumber,PiFirstTime Only,ProductKey

Data DigitalProductId,AuditMode, _

ProgramFilesPath,SM_AccessoriesName, _

PF_AccessoriesName

Data HWID,OtherDevicePath,ChannelFolderName, _

LinkFolderName,Plus! VersionNumber

Data BootCount,@

See Also

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

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