Friday, January 15, 2010

Vb 6.0 Registry Tutorial

How to access System registry? - An example


In windows operating system 'Registry' is an essential and critical system component in which windows stores application and user settings and preferences. For example if you want and application to run when you start windows you can instruct it in the registry in the following registry path,

\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

HKEY_CURRENT_USER stores user specific information. Applications can store user specific preferences and application states in this part.

In Visual Basic we have two functions, SaveSetting and GetSetting to Save the setting and get the setting from the registry. The settings will be saved in the following registry path: \HKEY_CURRENT_USER\Software\VB and VBA Program Settings.

The syntax of the GetSetting function is,

GetSetting (AppName as String, Section as String, Key as String, [Default]) as string

Where AppName is the application name, section is the registry section, key is the String/Binary/DWORD value.

The following tutorial will demonstrate the SaveSetting and GetSetting functions in VB 6.0

Open VB and create a Standard EXE project. Put a command button and set its name property to cmdSave and its caption property to save. Put a text box on the form and set its name property to txtSetting. Put another command button and set it's name property to cmdRead.

Double click cmdSave and write the following code,

Private Sub cmdSave_Click()
SaveSetting App.Title, "Example", "Test", txtSetting.Text
End Sub

Double click cmdRead and write the following code,

Private Sub cmdRead_Click()
MsgBox GetSetting(App.Title, "Example", "Test", "")
End Sub

Open the registry with regedit to see the results.

I have given Application Name / Title as 'RegistryDemo'. That is used as root entry for our application in VB and VBA program settings. "Example" is my setting entry or folder. Test is my key name with value 1234.









VB 6.0 offers only basic registry editing functions. If you want to perform advanced registry operation you must use Win32 registry functions which are more powerful. In VB .NET there is separate namespace with classes for registry editing.