Windows Registry
Name: Gaurang
Status: Student
Age: 20s
Location: N/A
Country: N/A
Date: Around 2001
Question:
I want to write QBasic program which can access and modify the windows
registry. Is this possible.
I guess one way of doing this is by opening the registry files such as
system.dat and user.dat in binary and then reading the file.
But I dont know the file format of the these files.
So, am I thinking right? Is there any other way of doing this?
Replies:
No, do not attempt to modify the registry settings in this way, you would
most likely end up trashing your registry. Instead, Windows provides an API
for reading, writing, and modifying the registry. The following is from
their documentation:
In Microsoft Windows 3.1 and earlier versions of Windows, program settings
like window positions, files used, and other items were commonly stored in
.ini files. In Windows NT, Windows 95, and later versions of Windows these
program settings are stored in the system registry.
Visual Basic provides a standard registry location for storing program
information for applications created in Visual Basic:
HKEY_CURRENT_USER\Software\VB and VBA Program Settings
\appname\section\key
Visual Basic also provides four statements and functions to manipulate the
settings stored in your application's registry location.
Function or Statement Description
------------------------------------------------
GetSetting function Retrieves registry settings.
------------------------------------------------
SaveSetting statement Saves or creates registry settings.
------------------------------------------------
GetAllSettings function Returns an array containing
multiple registry settings.
------------------------------------------------
DeleteSetting statement Deletes registry settings.
------------------------------------------------
Note To view registry entries, use the Regedit application, included with
Windows 95 and Windows NT.
To read more about using application settings, see the following topics:
Creating or Saving Application Settings Explains how to save new values
for registry keys stored in your application's registry location.
Retrieving Application Settings Explains how to retrieve registry values
stored in your application's registry location.
Deleting Application Settings
You can use the SaveSetting statement to save a new value for a registry key
stored in your application's registry location. For example, you could add
code to the Form_Unload event in the application's main form in order to
preserve settings at shutdown, or in the Form_Unload event on an Options
dialog box to update user preferences.
Use the following syntax for the SaveSetting statement:
SaveSetting appname, section, key, value
The following code saves new values for the Backup and LastEntry keys in the
Startup section of the registry for an application named "RegCust." This
code assumes that the variables strDate and intLastEntry contain the new
values.
Private Sub Form_Unload(Cancel As Integer)
SaveSetting "RegCust", "Startup", "Backup", strDate
SaveSetting "RegCust", "Startup", "LastEntry", _
intLastEntry
End Sub
If an entry for the application "RegCust" or any of these sections or keys
don't exist in the Software/Microsoft section in the registry, this code
will create it.
You can use the GetSetting and GetAllSettings functions to retrieve registry
values stored in your application's registry location. For example, your
application can retrieve registry settings to recreate its condition at the
time it was closed.
One Setting at a Time
To retrieve a single registry setting, use the following syntax for the
GetSetting function:
GetSetting(appname, section, key[, default])
The following code retrieves the value of the LastEntry key in the "RegCust"
application's Startup section, and displays the value in the Immediate
window.
Private Sub Form_Load()
Dim intLastEntry As Integer
intLastEntry = GetSetting("RegCust", "Startup", _
"LastEntry", "0")
Debug.Print intLastEntry
End Sub
Note that you can use the optional parameter, default, to set the value
returned by Visual Basic when there is no value listed in the registry for
the specified key.
Multiple Settings
To retrieve a list of registry keys and their values, use the following
syntax for the GetAllSettings function:
GetAllSettings(appname, section)
The following code retrieves a two-column list of registry keys and their
values in the "RegCust" application's Startup section, and displays the
results in the Immediate window.
Private Sub Form_Load()
Dim avntSettings As Variant
Dim intX As Integer
avntSettings = GetAllSettings("RegCust", "Startup")
For intX = 0 To UBound(avntSettings, 1)
Debug.Print avntSettings(intX, 0), _
avntSettings(intX, 1)
Next intX
End Sub
You can use the DeleteSetting statement to delete a registry key, section,
or an application's registry location. For example, you may want to delete
all registry information for an application when the application is
uninstalled.
Use the following syntax for the DeleteSetting statement:
DeleteSetting(appname, section, key)
The following code deletes the LastEntry key in the "RegCust" application's
Startup section.
Private Sub cmdDelKey_Click()
DeleteSetting "RegCust", "StartUp", "LastEntry"
End Sub
The following code deletes the "RegCust" application's entire Startup
section of the registry.
Private Sub cmdDelSection_Click()
DeleteSetting "RegCust", "StartUp"
End Sub
The following code deletes the entire registry location for the "RegCust"
application.
Private Sub cmdUnInstall_Click()
DeleteSetting "RegCust"
End Sub
Eric Tolman
Computer Scientist.
Click here to return to the Computer Science Archives