Get Setting Function

Named Arguments

Syntax

GetSetting(appname, section, key[, default]) appname

Use: Required

Data Type: String

The name of the application.

section

Use: Required

Data Type: String

The path from the application key to the key containing the value entries.

Use: Required Data Type: String

The name of the value entry whose value is to be returned.

default

Use: Optional

Data Type: String

The value to return if no value can be found. Return Value

A string containing the value of the specified key; default if key, section, or appname aren't found.

Description

Returns a single value from a specified section of your application's entry in the HKEY_CURRENT_USER\Software\VB and VBA Program Settings\ branch of the registry.

Rules at a Glance

• If appname, section, or key isn't found in the registry, GetSetting returns default.

• If default is omitted, it's assumed to be a zero-length string ("").

• The function retrieves a value from a subkey of the KEY_CURRENT_ USER\Software\VB and VBA Program Settings key of the registry.

• section need not be an immediate subkey of appname; instead, section can be a fully qualified path to a nested subkey, with each subkey separated from its parent by a backslash. For example, a value of Settings\Coordinates for the section argument indicates that the value is to be retrieved from HKEY_

GetSetting Function 361

CURRENT_USER\Software\VB and VBA Program Settings\appname\Set-tings\Coordinates.

Programming Tips & Gotchas

• GetSetting is a function that was developed to retrieve data from initialization files in 16-bit environments and to retrieve data from the registry under Win9x and WinNT. The language of the official documentation, however, reflects the language of initialization files. The arguments labeled appname and section are in fact registry keys; the argument labeled key is in fact a registry value entry.

• Because the built-in registry functions in VB create only string type registry value entries, GetSetting and GetAllSettings return string values. Therefore, before you use numeric values returned from the registry, you should convert the value to a numeric data type explicitly using the appropriate conversion function.

• Use the App object's EXEName property to pass your application's name to the GetSetting function as the value of the appname parameter both when reading and writing registry data.

• The built-in registry manipulation functions allow you to create professional 32-bit applications that use the registry for holding application-specific data, in the same way .INI files were used in the 16-bit environment. You can, for example, store information about the user's desktop settings (i.e., the size and position of forms) from the last time the program was run.

• GetSetting, GetAllSettings, and SaveSettings allow you direct access to only a limited section of the windows registry, that being a special branch created for your application (HKEY_CURRENT_USER\Software\VB and VBA Program Settings\yourappname). You can't access or change other registry settings without resorting to the Windows API.

• GetSetting doesn't allow you to retrieve the default value of a registry key. Attempting to do so produces runtime error 5, "Invalid procedure call or argument." This isn't as great a limitation as it may appear, since SaveSetting also can't write a default value to a registry key.

• Because GetSetting retrieves data from the user branch of the registry, and the physical file that forms the user branch of the registry may change (depending, of course, on who the user is and, in the case of Win9x systems, whether or not the system is configured to support multiple users), never assume that an application has already written data to the registry. In other words, even if you're sure your application's installation routine or the application itself has successfully stored values in the registry, always supply a meaningful value for the default argument.

• Only those settings that are created using the Windows API or the SaveSet-ting function are returned. In other words, a VB application doesn't have a registry entry unless you have create one explicitly.

• Although GetSetting writes string data only to the registry, you can use a variable of almost any data type to retrieve it. The GetSetting function automatically handles the conversion of string data to the data type of the variable to

362 Chapter 7- The Language Reference which the return value of GetSetting is assigned. The sole exceptions are user-defined data types and arrays of byte data.

See Also

DeleteSetting Statement, GetAllSettings Function, SaveSetting Statement

+1 0

Post a comment