WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

Reading and Writing Registry Values with Visual Basic

by Ron Petrusha
07/27/2004

The Windows system Registry can be likened to a file system. Like a file system, it is organized hierarchically. Just as the file system has a root folder that contains one or more child folders, each of which in turn contains one or more child folders, and so on, the Registry has a top-level key that contains one or more child keys, each of which in turn contains one or more child keys, and so on. (Unlike the file system, though, the Registry has either five or six top-level keys, depending on the version of Windows you're using.) Just as the purpose of a folder is to store files, so the purpose of a Registry key is in part to store values.

A Registry value itself more closely resembles an item in a collection than it does a file. Like the member of a collection, a Registry value has a data type. The following are the data types the Registry supports:

Also like members of collections, most Registry values are named. Figure 1, for example, shows that two named values are stored in the HKEY_CLASSES_ROOT\.txt key. The first, Content Type, is a string (REG_SZ) whose value is "txtfile". The second, PerceivedType, is also a string; its value is "text".

Figure 1 shows one additional value, indicated by the name "(Default)". A remnant of the Registry in 16-bit Windows systems, this is the default or unnamed value. As Figure 1 shows, it also is a string, or REG_SZ, that has been assigned the value "txtfile". There can be only one unnamed value per Registry key. A Registry key, then, while it has a value that is of a definite data type, may or may not have a name.

Click for larger view
Figure 1. A registry key with its values (You can click on the screen shot to open a full-size view.)

When you write a particular value to the Registry, you know precisely the name of the value, its data type, and the data you'd like to assign to it. Although this is sometimes the case when reading a particular Registry value, it also very frequently happens that you don't know the precise names of Registry values (if they have names at all), nor do you know their data types or their values. In this article, we'll show how to deal with all three of these common scenarios, as well as show you how to write values to the Registry.

Retrieving Known Registry Values

When you know the name and the data type of the value that you want to retrieve, you can retrieve it directly. To do this, you simply open the key containing the value, then retrieve the value itself.

For instance, suppose that you're developing a Registry utility and you intend to respect the wishes of the system administrator, who may have disabled the use of Registry tools for a particular user. In that case, you know that you want to retrieve the DisableRegistryTools value of the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System key. You know that DisableRegistryTools is a Long (a REG_DWORD) that can take one of two possible values: 0 indicates that the user can have access to Registry tools, while 1 indicates that he or she should be prevented from directly accessing the Registry. Since both the value and the key are typically created by System Policy Editor when a user's Registry access is blocked, you must also be prepared to handle cases when either the key or the value is not present. The following IsRegistryEditable function does that:

' Determines whether Registry tools have been disabled for the current user
Public Function IsRegistryEditable() As Boolean

Dim lValue As Long            ' Variable for value
Dim sKey As String            ' Key to open
Dim hKey As Long              ' Handle to registry key

sKey = "Software\Microsoft\Windows\CurrentVersion\Policies\System"

If RegOpenKeyEx(HKEY_CURRENT_USER, sKey, 0, KEY_READ, hKey) <> ERROR_SUCCESS Then
   ' Key does not exist, return True
   IsRegistryEditable = True
Else
   ' Determine if value exists
   If RegQueryValueEx(hKey, _
                      "DisableRegistryTools", _
                      0, _
                      REG_DWORD, _
                      lValue, _
                      Len(lValue) _
                     ) <> ERROR_SUCCESS Then
      ' value does not exist, return True
      IsRegistryEditable = True
   Else
      ' Return opposite of value (0 = Editable, 1 = Disable)
      IsRegistryEditable = Not CBool(lValue)
   End If
End If

End Function

Since we know what we're looking for in the Registry, we simply call RegOpenKeyEx to open the Registry key containing the value we want. If the function fails, it does not return ERROR_SUCCESS, so we know that something has gone wrong. (For details on RegOpenKeyEx, see Reading and Writing Registry Keys with Visual Basic). If the key is opened successfully, we call the RegQueryValueEx function, which retrieves the value. Its syntax is:

Public Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" ( _
   ByVal hKey As Long, _                  ' Handle to open registry key
   ByVal lpValueName As String, _         ' Name of value
   ByVal lpReserved As Long, _            ' Reserved, must be 0 
   lpType As Long, _                      ' Registry data type constant
   lpData As Any, _                       ' By reference variable for data
   lpcbData As Long _                     ' Number of bytes of data written 
) As Long

Two of the parameters, lpData and lpcbData, are passed to the function by reference. When the function returns, lpData will contain the value's data, while lpcbData will indicate the number of bytes written to lpData. This allows us to allocate a buffer that is large enough to hold the data and to know how much of the buffer must be trimmed to extract the actual data. In our case, since we're reading a long integer (a Visual Basic Long or a Registry REG_DWORD), we don't need to be concerned about the size of our buffer and possible buffer overflows. Similarly, we've provided a literal value, Len(lValue), rather than passing a variable by reference as the lpcbData argument, since we already know how many bytes will be written when we retrieve a REG_DWORD.

Often when retrieving values from the Registry, our code has to handle the failure to find a particular key. In the case of the IsRegsitryEditable function, for instance, the function returns True, indicating that the Registry can be accessed by user tools, if the key or value does not exist.

While this example is straightforward enough, in other cases we know which key we want to access but either aren't certain what kind of data it holds or don't know what the size of that data is. Imagine, for instance, that you're developing a utility that launches the application capable of handling a file-system object the user selects. In this case, we'll simply extract the extension from the file that the user has selected, then use its file extension to form the path to a file identification key. Once we open the file extension key, we can retrieve its unnamed value, which is the name of its file association key. From there, we can retrieve the value of the Shell\Open\Command subkey to retrieve the name of the application capable of handling the file. The following code accomplishes this:

Public Function GetAssociatedApp(sExten As String) As String

   Dim sBuffer As String, sProgName As String
   Dim sPath As String
   Dim lBuffer As Long, lProgName As Long
   Dim hKey As Long, hProgKey As Long

   sBuffer = Space(20)
   lBuffer = Len(sBuffer)

   ' Open Key
   If RegOpenKeyEx(HKEY_CLASSES_ROOT, sExten, 0, KEY_READ, hKey) <> ERROR_SUCCESS Then
      ' Key does not exist, return null string
      GetAssociatedApp = vbNullString
   Else
      Dim lType As Long
      
      ' Get key's unnamed value
      RegQueryValueEx hKey, vbNullString, 0, 0, ByVal sBuffer, lBuffer
      RegCloseKey hKey
      sBuffer = Left(sBuffer, lBuffer - 1)
      
      ' Open Command key of File Association key's Open subkey
      sPath = sBuffer & "\shell\open\command"
      If RegOpenKeyEx(HKEY_CLASSES_ROOT, sPath, 0, KEY_READ, hProgKey) = ERROR_SUCCESS Then
         ' Determine data type and buffer size of key
         RegQueryValueEx hProgKey, vbNullString, 0, 0, ByVal vbNull, lProgName
      
         ' Retrieve file association
         sProgName = Space(lProgName + 1)
         RegQueryValueEx hProgKey, vbNullString, 0, lType, ByVal sProgName, lProgName
         RegCloseKey hProgKey
         sProgName = Left(sProgName, lProgName - 1)
      
         ' Check if environment string is present
         If lType = REG_EXPAND_SZ Then
            Dim lProg As Long
            Dim sProg As String
         
            sProg = Space(MAX_PATH + 1)
            lProg = ExpandEnvironmentStrings(sProgName, sProg, Len(sProg))
            sProgName = Left(sProg, lProg - 1)
         End If
         GetAssociatedApp = sProgName
      Else
         GetAssociatedApp = ""
      End If
   End If
End Function

The first part of this code is quite similar to the code in the previous example. Because we know that our code is looking up a file extension, we know that we want to open the HKEY_CLASSES_ROOT\<file extension> key and read its unnamed value. We know that that value will be a string that we can use to form a path to the next Registry key we'd like to open, HKEY_CLASSES_ROOT\<file identifier>\shell\open\command, and that we'd like to retrieve the latter key's unnamed value.

Although there is some similarity to the code in the previous example, there are also some salient differences. Whereas we were working with a named value in the previous example, here we're attempting to retrieve the unnamed value of a key. Because of this, we pass vbNullString as an argument in place of a value name. Second, because we're passing string data to and from the function, we must override Visual Basic's default method of passing strings by reference by providing the ByVal keyword. Third, because a string's length is variable, we must allocate a buffer that's sufficiently large to hold all the string data plus a null character to mark the end of the string. When the function returns, we must trim our string so that it contains only characters actually written to the buffer by the function.

Once we open the HKEY_CLASSES_ROOT\<file extension>\shell\open\command key and are about to get the name of the program responsible for handling a particular file type, we're no longer completely sure of the format of the value that we're looking for in the Registry. We don't know how long the unnamed value of the key is, nor do we know precisely what type of string it contains. Along with standard strings (of type REG_SZ), there are also strings with embedded "macros" (REG_EXPAND_SZ) and string arrays (REG_MULTI_SZ).

To ensure that we allocate a buffer large enough to retrieve the program name, we call RegQueryValueEx twice. In the first call, we provide null values for all but one by reference argument, the number of characters written to the buffer. This preliminary function call allows us to determine how large a buffer we need to allocate in order to successfully retrieve the data. Note that the value of lpcbData includes the length not only of the data, but also of the terminating null character.

Finally, although we know in advance that we are handling string data here, we must check the precise type of the data. Along with conventional string data, Registry strings can include environmental variables (like %SystemRoot% and %WinDir%) that need to be expanded into their values. To do this, we call the ExpandEnvironmentStrings function if the result of the RegQueryValueEx function call indicates that the function has returned REG_EXPAND_SZ data.

Pages: 1, 2

Next Pagearrow