Check Registration Entries at Start-Up


If your application has an entry in the Registry, it should check its registered entries at start-up to verify that the .EXE's file path has not changed since the application last ran. If the .EXE's current file path does not match the registered file path, then the application should update its registration.

Visual Basic does not provide built-in support for checking system registration entries, so you must use Windows API functions. Table 19.4 lists the functions Windows provides to help you maintain Registry entries.

Table 19.4 - Windows Registry Functions

Function Use to
RegCloseKey Close a key after opening it with RegOpenKey
RegCreateKey Create a new key
RegDeleteKey Delete an existing key
RegEnumKey Get the next subkey of a specified key
RegOpenKey Open a key
RegQueryValue Retrieve the value setting of a key as a text string
RegSetValue Set the value of a key

To check a registered key in the Registry, follow these steps:

  1. Use RegOpenKey to open the registration key that contains path and file name information. For example, "olestore.application\shell\open."
  2. Use RegQueryValue to return the value of the subentry containing path and file name information.
  3. Compare the returned value to the application's current path and file name. If the two values don't match, use RegSetValue to change the registered value of the subentry.
  4. Use RegCloseKey to close the registration key opened in step 1.

The CheckRegistrationEntry procedure in Listing 19.23 shows the declarations for the system registration Windows API functions, and demonstrates how to check the registration entry for the OLESTORE application.

Listing 19.23 - Checking Registration Entries on Start-Up


Declare Function RegOpenKey Lib "Shell32" _
   (ByVal HKeyIn As Long, ByVal LPCSTR As String, HKeyOut As Long) As Long
Declare Function RegCloseKey Lib "Shell32" _
   (ByVal HKeyIn As Long) As Long
Declare Function RegEnumKey Lib "Shell32" _
   (ByVal HKeyIn As Long, ByVal SubKeyIn As Long, _
   ByVal KeyName As String, ByVal KeyNameLen As Long) As Long
Declare Function RegQueryValue Lib "Shell32" _
   (ByVal HKeyIn As Long, ByVal SubKey As String, _
   ByVal KeyValue As String, KeyValueLen As Long) As Long
Declare Function RegSetValue Lib "Shell32" _
   (ByVal HKeyIn As Long, ByVal SubKey As String, _
   ByVal lType As Long, ByVal strNewValue As String, _
   ByVal lIngnored As Long) As Long
Declare Function RegDeleteKey Lib "Shell32" _
   (ByVal HKeyIn As Long, ByVal SubKeyName As String)
Const HKEY_CLASSES_ROOT = 1
Sub CheckRegistrationEntry()
   Dim hkroot As Long, x As Long, lLen As Long
   Dim strKeyID As String, strKeyDesc As String
   Dim strSearchKey As String
   Dim strAppName As String
   ' Get current application path and file name.
   strAppName = App.Path & "\" & App.EXEName & ".EXE"
   lLen = 80
   ' Specify registration key to check.
   strSearchKey = "olestore.1\shell\open"
   ' Specify subentry value to check.
   strKeyID = "command"
   ' Initalize key description (value returned by RegQueryValue).
   strKeyDesc = String(lLen, 0)
   ' Open the registration key.
   x = RegOpenKey(HKEY_CLASSES_ROOT, strSearchKey, hkroot)
   ' Get the value of the "command" subentry.
   x = RegQueryValue(hkroot, strKeyID, strKeyDesc, lLen)
   ' Check the value against the current installation.
   If strKeyDesc <> strAppName Then
           ' If it doesn't match, change the registered value.
           x = RegSetValue(hkroot, strKeyID, 1, strAppName, 0)
   End If
   ' Close the registration key.
   x = RegCloseKey(hkroot)
End Sub