Managing Initialization Files and the Windows Registry

About this chapter

This chapter describes how to manage preferences and default settings for PowerBuilder applications.

About preferences and default settings

Many PowerBuilder applications store user preferences and default settings across sessions. For example, many applications keep track of settings that control the appearance and behavior of the application, or store default parameters for connecting to the database. PowerBuilder applications can manage this kind of information in initialization files or in the Windows registry.

Database connection parameters

Often you need to set the values of the Transaction object from an external file. For example, you might want to retrieve values from your PowerBuilder initialization file when you are developing the application or from an application-specific initialization file when you distribute the application.

For information about database connection parameters in an initialization file, see Reading values from an external file.

For an example of how to save and restore database connection parameters in the Windows registry, see Managing information in the Windows registry.

Toolbar settings

PowerBuilder provides some functions you can use to retrieve information about your toolbar settings and also modify these settings. By using these functions, you can save and restore the current toolbar settings.

For more information, see Saving and restoring toolbar settings.

Other settings you may want to save

In addition to the database connection parameters and toolbar settings, you may want to store a variety of other application-specific settings. For example, you might want to keep track of user preferences for colors, fonts, and other display settings.

Managing information in initialization files

Functions for accessing initialization files

PowerBuilder provides several functions you can use to manage application settings in initialization files.

Function

Description

ProfileInt

Obtains the integer value of a setting in a profile file

ProfileString

Obtains the string value of a setting in a profile file

SetProfileString

Writes a value in a profile file


For complete information about these functions, see PowerScript Reference.

For how to use the ProfileString functions with the registry, see Managing information in the Windows registry.

The format of APP.INI

The examples below manage application information in a profile file called APP.INI. This file keeps track of user preferences that control the appearance of the application. It has a Preferences section that stores four color settings:

[Preferences]
WindowColor=Silver
BorderColor=Red
BackColor=Black
TextColor=White

Reading values

The following script retrieves color settings from the APP.INI file:

wincolor = ProfileString("app.ini", "Preferences", "WindowColor", "")
brdcolor = ProfileString("app.ini", "Preferences", "BorderColor", "")
bckcolor = ProfileString("app.ini", "Preferences", "BackColor", "")
txtcolor = ProfileString("app.ini", "Preferences", "TextColor", "")

Setting values

The following script stores color settings in the APP.INI file:

SetProfileString("app.ini", "Preferences", "WindowColor", wincolor)
SetProfileString("app.ini", "Preferences", "BorderColor", brdcolor)
SetProfileString("app.ini", "Preferences", "BackColor", bckcolor)
SetProfileString("app.ini", "Preferences", "TextColor", txtcolor)

Managing information in the Windows registry

Functions for accessing the Registry

PowerBuilder provides several functions you can use to manage application settings in the Windows registry.

Function

Description

RegistryDelete

Deletes a key or a value in a key in the Windows registry.

RegistryGet

Gets a value from the Windows registry.

RegistryKeys

Obtains a list of the keys that are child items (subkeys) one level below a key in the Windows registry.

RegistrySet

Sets the value for a key and value name in the Windows registry. If the key or value name does not exist, RegistrySet creates a new key or value name.

RegistryValues

Obtains a list of named values associated with a key.


For the complete information for these functions, see the PowerScript Reference.

Overriding initialization files

You can use the ProfileString functions to obtain information from the registry instead of from an initialization file. Create a new key called INIFILEMAPPING at the following location:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion

To override the WIN.INI file, create a subkey in INIFILEMAPPING called WIN.INI with the following value:

#usr:software\microsoft\windows\currentversion\extensions

The examples that follow use the registry to keep track of database connection parameters. The connection parameters are maintained in the registry in the MyCo\MyApp\database branch under HKEY_CURRENT_USER\Software.

Reading values from the registry

The following script retrieves values for the default Transaction object from the registry.

RegistryGet("HKEY_CURRENT_USER\Software\MyCo\MyApp\database", &
   "dbms", sqlca.DBMS)
RegistryGet("HKEY_CURRENT_USER\Software\MyCo\MyApp\database", &
    "database", sqlca.database)
RegistryGet("HKEY_CURRENT_USER\Software\MyCo\MyApp\database", &
    "userid", sqlca.userid)
RegistryGet("HKEY_CURRENT_USER\Software\MyCo\MyApp\database", &
    "dbpass", sqlca.dbpass)
RegistryGet("HKEY_CURRENT_USER\Software\MyCo\MyApp\database", &
    "logid", sqlca.logid)
RegistryGet("HKEY_CURRENT_USER\Software\MyCo\MyApp\database", &
    "logpass", sqlca.logpass)
RegistryGet("HKEY_CURRENT_USER\Software\MyCo\MyApp\database", &
    �servername", sqlca.servername)
RegistryGet("HKEY_CURRENT_USER\Software\MyCo\MyApp\database", &
    "dbparm", sqlca.dbparm)

Setting values in the registry

The following script stores the values for the Transaction object in the registry:

RegistrySet("HKEY_CURRENT_USER\Software\MyCo\MyApp\database", &
    "dbms", sqlca.DBMS)
RegistrySet("HKEY_CURRENT_USER\Software\MyCo\MyApp\database", &
    "database", sqlca.database)
RegistrySet("HKEY_CURRENT_USER\Software\MyCo\MyApp\database", &
    "userid", sqlca.userid)
RegistrySet("HKEY_CURRENT_USER\Software\MyCo\MyApp\database", &
    "dbpass", sqlca.dbpass)
RegistrySet("HKEY_CURRENT_USER\Software\MyCo\MyApp\database", &
    "logid", sqlca.logid)
RegistrySet("HKEY_CURRENT_USER\Software\MyCo\MyApp\database", &
    "logpass", sqlca.logpass)
RegistrySet("HKEY_CURRENT_USER\Software\MyCo\MyApp\database", &
    "servername", sqlca.servername)
RegistrySet("HKEY_CURRENT_USER\Software\MyCo\MyApp\database", &
    "dbparm", sqlca.dbparm)