Windows Registry VB6

From Free Knowledge Base- The DUCK Project: information for everyone
Revision as of 07:20, 2 February 2008 by Admin (Talk | contribs)

Jump to: navigation, search

You can read from and write to the Windows Registry with your Visual BASIC program. There are basically two main approaches to accomplishing this in your Visual BASIC program.

  • Using Visual BASIC Built in Registry Functions
  • Advanced Windows 32 Base API DLL (advapi32.dll)

What's the difference?

Using built in registry functions limits you a great deal. Using the API allows you complete access to the registry from an application level. Using the built in functions is quick and easy. Using the API requires a bit more work and external function calls. Using the built in functions is safer.

Visual BASIC Built In Registry Functions

The functions are :

  • GetSetting
  • SaveSetting

SaveSetting stores the data in the Registry (in HKEY_CURRENT_USER|Software|VB and VBA Program Settings|YourAppName). The four parts of the function are the name under which it is stored. That is the main limitation, being where the data is stored. All of your registry data for your application has to be under "VB and VBA Program Settings" under the HKEY_CURRENT_USER hive.

Parameters

  • AppName: Required. String expression containing the name of the application or project to which the setting applies.
  • Section: Required. String expression containing the name of the section in which the key setting is being saved.
  • Key: Required. String expression containing the name of the key setting being saved.
  • Setting: Required. Expression containing the value to which Key is being set.

The SaveSetting function adds the key to HKEY_CURRENT_USER\Software\VB and VBA Program Settings. SaveSetting requires that a user be logged on since it operates under the HKEY_LOCAL_USER registry key, which is not active until a user logs on interactively.

HKEY_CURRENT_USER\Software\VB and VBA

Code Example:

Private Sub Form_Load()
  Me.Left = GetSetting(App.Title, "Settings", "MainLeft", 1000)
  Me.Top = GetSetting(App.Title, "Settings", "MainTop", 1000)
  Me.Width = GetSetting(App.Title, "Settings", "MainWidth", 6500)
  Me.Height = GetSetting(App.Title, "Settings", "MainHeight", 6500)
End Sub
Private Sub Form_Unload(Cancel As Integer)
  If Me.WindowState <> vbMinimized Then
    SaveSetting App.Title, "Settings", "MainLeft", Me.Left
    SaveSetting App.Title, "Settings", "MainTop", Me.Top
    SaveSetting App.Title, "Settings", "MainWidth", Me.Width
    SaveSetting App.Title, "Settings", "MainHeight", Me.Height
  End If
End Sub

 

Advanced Windows 32 Base API DLL (advapi32.dll)