Accessing the Registry With the Windows API

When the VB functions and statements for accessing the Registry don't give you the flexibility you desire, you'll probably have to turn to the Windows API. The API actually has quite a few functions that deal directly with the Registry. they are all listed here, but only the more useful ones are covered in any detail.

Table 32.6. The Windows API System Registry functions.

Function Name

Description

RegCloseKey Closes an open key.
RegConnectRegistry Opens a key on a remote system.
RegCreateKey Creates a new key.
RegCreateKeyEx Creates a new key or opens an existing key.
RegDeleteKey Deletes a key.
RegDeleteValue Deletes a key's value.
RegEnumKey Returns a list of the subkeys of a given key.
RegEnumKeyEx Returns a list of the subkeys of a given key. Gives you more control than RegEnumKey.
RegEnumValue Returns a list of the values for a given key.
RegFlushKey Makes sure that changes to keys have been written to disk.
RegGetKeySecurity Returns the security information for a key.
RegLoadKey Loads Registry information from a disk file.
RegNotifyChangeKeyValue Notifies a program when a key's value changes.
RegOpenKey Opens a key.
RegOpenKeyEx Opens a key. Gives you more control than RegOpenKey.
RegQueryInfoKey Retrieves information about a key.
RegQueryValue Retrieves a key value.
RegQueryValueEx Retrieves a key value. Gives you more control than RegQueryValue.
RegReplaceKey Replaces Registry information with Registry data from a disk file.
RegRestoreKey Restores Registry information from a disk file.
RegSaveKey Saves Registry information to a disk file.
RegSetKeySecurity Sets security information for a key.
RegSetValue Sets a key value.
RegSetValueEx Sets a key value. Gives you more control than RegSetValue.
RegUnloadKey Closes an open key on a remote system.
As you can see by Table 32.6, the Windows API gives you a far greater control of System Registry manipulation than the VB functions. With the API, you can access security information for keys, manage keys on remote systems, or transfer Registry information to and from disk files.

The API functions that will be covered in this section include RegCloseKey, RegCreateKey, RegDeleteValue, RegOpenKeyEx, RegQueryValueEx, and RegSetValueEx. If you want more information on any of the other functions, Dan Appleman's Visual Basic Programmer's Guide to the Win32 API is strongly recommended.

RegOpenKeyEx
Declare Function RegOpenKeyEx Lib "advapi32"
    Alias "RegOpenKeyExA"
    (ByVal hKey As Long,
     ByVal lpSubKey As String, 
     ByVal ulOptions As Long,
     ByVal samDesired As Long,
     phkResult As Long) As Long
Before a Registry subkey can be accessed, it must first be opened. The RegOpenKeyEx API function opens a subkey and returns its handle, a unique number that is used to reference the subkey. If the function is successful, the handle is placed in the variable specified as the phkResult argument.

The first argument to the function, hKey, refers to the handle of the subkey that precedes the subkey to be opened. For example, if you were using a subkey path such as HKEY_CURRENT_USER\Software\
MyApplication, the handle for the Software subkey would have to be given before the MyApplication subkey could be opened.

The lpSubKey argument specifies the name of the subkey to open. Continuing with the example given above, this would be "MyApplication".

The ulOptions argument is not used and should be set to zero. The samDesired argument specifies the types of operations that can be performed on the subkey. Table 32.7 lists some of the constants that can be used for the samDesired argument and their values.

Table 32.7. Constants that can be used with the ulOptions argument.

Constant

Value

KEY_EVENT &H1
KEY_QUERY_VALUE &H1
KEY_SET_VALUE &H2
KEY_CREATE_SUB_KEY &H4
KEY_ENUMERATE_SUB_KEYS &H8
KEY_NOTIFY &H10
KEY_CREATE_LINK &H20
KEY_ALL_ACCESS &H3F
The RegOpenKeyEx function returns a zero value if the subkey can be opened and non-zero if there is a problem.
RegCloseKey
Declare Function RegCloseKey Lib "advapi32"
    (ByVal hKey As Long) As Long
The RegCloseKey API function is used to close a currently open subkey. Its only argument is hKey, which specifies the handle of the open subkey. RegCloseKey returns zero if successful and non-zero on failure.
RegCreateKey
Declare Function RegCreateKey Lib "advapi32"
    Alias "RegCreateKeyA" 
    (ByVal hKey As Long, 
     ByVal lpSubKey As String,
     phkResult As Long) As Long
To create a new subkey, first make sure the subkey that will contain it has been opened with the RegOpenKeyEx function. You'll need the handle of that subkey to pass to the RegCreateKey function in the hKey argument. The lpSubKey argument is the name of the key value, and phkResult is the assigned the handle of the new subkey (provided the function is successful). The function will return a zero value on success or non-zero on failure.
RegDeleteKey
Declare Function RegDeleteKey Lib "advapi32"
    Alias "RegDeleteKeyA"
    (ByVal hKey As Long,
     ByVal lpSubKey As String) As Long
To delete a subkey, use the RegDeleteKey API function. Simply pass it the handle of the open subkey that contains the subkey to be deleted in the hKey argument, and the name of the subkey to be deleted in the lpSubKey argument. The functions returns zero if successful and non-zero on failure.
RegDeleteValue
Declare Function RegDeleteValue Lib "advapi32"
    Alias "RegDeleteValueA"
    (ByVal hKey As Long,
     ByVal lpValueName As String) As Long
To delete a key value, use the RegDeleteValue. Simply pass it the handle of the subkey that contains the value in hKey, and the name of the key value in lpValueName. The function returns zero if successful and non-zero if it fails.
RegQueryValueEx
Declare Function RegQueryValueEx Lib "advapi32"
    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
To retrieve the value of a key, use the RegQueryValueEx function. You must first open the subkey that contains the key value so you can pass its handle to this function as the hKey argument. The lpValueName argument specifies the name of the text value. lpReserved is not used and should be set to 0. The type of data to be retrieved is specified by the lpType argument (see Table 32.8), and the lpData argument specifies a buffer into which the value will be placed. The lpcbData argument should indicate the size of the buffer. Zero is returned by the function if successful and non-zero is returned on failure.

Table 32.8. The constants for specifying key value types.

Constant

Value

Description

REG_NONE 0 Undefined
REG_SZ 1 String (Null terminated)
REG_EXPAND_SZ 2 Non-expanded environment string
REG_BINARY 3 Binary data
REG_DWORD 4 32-bit
REG_DWORD_LITTLE_ENDIAN 4 32-bit with high-order byte first
REG_DWORD_BIG_ENDIAN 5 32-bit with low-order byte first
REG_LINK 6 Symbolic link
REG_MULTI_SZ 7 Multiple strings
REG_RESOURCE_LIST 8 Resource list for device driver
RegSetValueEx
Declare Function RegSetValueEx Lib "advapi32"
    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
The RegSetValueEx API function sets a key value. hKey is the handle of the subkey that contains the key value, and lpValueName is the name of the value itself. Reserved is not used and should be set to 0. The type of value is specified by dwType (see Table 32.8), and lpData is the buffer that contains the value. The size of the buffer is specified by cbData. The function returns zero on success and non-zero if unsuccessful.
Top Home