Windows Registry VB6

From Free Knowledge Base- The DUCK Project: information for everyone
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)

RegQueryValueEx

  • hKey identifies a currently opened key
  • lpValueName contains the name of the value to be queried
  • lpReserved must be null
  • lpType will receive key's value type
  • lpData points to a buffer that receives the value’s data
  • lpcbData specifies the size, in bytes, of the buffer pointed to by the lpData parameter

Step 1:
Create a MODULE called "modREgKeYmAker" (or whatever you like) added to your VB6 Project

Step 2:
Cut and paste the following code:

 Option Explicit
 
 Declare Function RegCloseKey Lib "advapi32.dll" (ByVal HKEY As Long) As Long
 Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal HKEY As Long, ByVal lpSubKey As String, phkResult As Long) As Long
 Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal HKEY As Long, ByVal lpSubKey As String) As Long
 Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal HKEY As Long, ByVal lpValueName As String) As Long
 Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal HKEY As Long, ByVal lpSubKey As String, phkResult As Long) As Long
 Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal HKEY As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
 Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal HKEY As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
 
 Public Const HKEY_CLASSES_ROOT = &H80000000
 Public Const HKEY_CURRENT_USER = &H80000001
 Public Const HKEY_LOCAL_MACHINE = &H80000002
 Public Const HKEY_USERS = &H80000003
 Public Const HKEY_PERFORMANCE_DATA = &H80000004
 Public Const REG_SZ = 1
 
 Private Const ERROR_SUCCESS As Long = 0
 
 Function RegQueryStringValue(ByVal HKEY As Long, ByVal strValueName As String)
     Dim frmMain As Form
     Dim lResult As Long
     Dim lValueType As Long
     Dim strBuf As String
     Dim lDataBufSize As Long
         On Error GoTo 0
     lResult = RegQueryValueEx(HKEY, strValueName, 0&, lValueType, ByVal 0&, lDataBufSize)
     If lResult = ERROR_SUCCESS Then
         If lValueType = REG_SZ Then
             strBuf = String(lDataBufSize, " ")
             lResult = RegQueryValueEx(HKEY, strValueName, 0&, 0&, ByVal strBuf, lDataBufSize)
             If lResult = ERROR_SUCCESS Then
                 RegQueryStringValue = StripTerminator(strBuf)
             End If
         End If
     End If
 End Function
 
 Public Function GetSettingEx(HKEY As Long, sPath As String, sValue As String)
     Dim KeyHand&
     Dim datatype&
     Call RegOpenKey(HKEY, sPath, KeyHand&)
     GetSettingEx = RegQueryStringValue(KeyHand&, sValue)
     Call RegCloseKey(KeyHand&)
 End Function
 
 Function StripTerminator(ByVal strString As String) As String
     Dim intZeroPos As Integer
 
     intZeroPos = InStr(strString, Chr$(0))
     If intZeroPos > 0 Then
         StripTerminator = Left$(strString, intZeroPos - 1)
     Else
         StripTerminator = strString
     End If
 End Function
 
 Public Sub SaveSettingEx(HKEY As Long, sPath As String, sValue As String, sData As String)
     Dim KeyHand As Long
     Call RegCreateKey(HKEY, sPath, KeyHand)
     Call RegSetValueEx(KeyHand&, sValue, 0, REG_SZ, ByVal sData, Len(sData))
     Call RegCloseKey(KeyHand&)
 End Sub

Step 3:
Usage.

Here are some usage examples:

Retreiving Registry Info

 'REGISTRY: load default storybook open path
 strRetval = modREgKeYmAker.GetSettingEx(HKEY_CURRENT_USER, "Software\My Software Name\s2album", "DefaultStorybookPath")
 If strRetval = "" Then strStorytelling = strMyDocuments & "\EA Games\The Sims 2\Storytelling" Else strStorytelling = strRetval
   
 'REGISTRY: SetDefaultToLastUsedPath
 strRetval = modREgKeYmAker.GetSettingEx(HKEY_CURRENT_USER, "Software\My Software Name\s2album", "SetDefaultToLastUsedPath")
 If strRetval = "True" Then blnSetDefaultToLastUsedPath = True Else blnSetDefaultToLastUsedPath = False
 
 'REGISTRY: LoadImageCacheWarning is for strShowICWOption and blnShowICW
 strRetval = modREgKeYmAker.GetSettingEx(HKEY_CURRENT_USER, "Software\My Software Name\s2album", "LoadImageCacheWarning")
 If strRetval = "" Then strShowICWOption = "True" Else strShowICWOption = strRetval
 If strShowICWOption = "True" Then blnShowICW = True Else blnShowICW = False

Setting Registry Info

 modREgKeYmAker.SaveSettingEx HKEY_CURRENT_USER, "Software\My Software Name\s2album", "DefaultStorybookPath", strStorytelling
 
 modREgKeYmAker.SaveSettingEx HKEY_CURRENT_USER, "Software\My Software Name\s2album", "SetDefaultToLastUsedPath", strSetDefaultToLastUsedPath
 
 modREgKeYmAker.SaveSettingEx HKEY_CURRENT_USER, "Software\My Software Name\s2album", "LoadImageCacheWarning", strShowICWOption