dcsimg
December 9, 2016
Hot Topics:

Accessing the Windows Registry with the API

  • November 19, 2002
  • By John Percival
  • Send Email »
  • More Articles »

Copy the following procedure into your project.

Public Sub DeleteValue( _
       ByVal hKey As Long, _
       ByVal strPath As String, _
       ByVal strValue As String)
Dim hCurKey As Long
Dim lRegResult As Long

lRegResult = RegOpenKey(hKey, _
                strPath, hCurKey)

lRegResult = RegDeleteValue(hCurKey, _
                            strValue)

lRegResult = RegCloseKey(hCurKey)

End Sub

The RegOpenKey API function to create takes a handle to a key, and the path that you want to select, and returns a handle to the new key. As was mentioned earlier, a handle to a key is a reference to a key. You must have a key to start with, so the six constants for the six root keys are fixed, and are provided as constants. The handles to these keys are the same on every Win32 system in every session. The other handles that you receive for the sub-keys will be variable, and will change each time you select the key. However, the handle will remain valid until you use the RegCloseKey function. If you don't close the handle to the key when you are finished with it, it will continue using up resources, so you must remember to close it when you are done.

Unusually, the Registry API functions will return 0 on success, and another number for failure. There is the constant ERROR_SUCCESS, which is defined to make code more readable when debugging.

This wrapper function takes a handle to a key, a path to a key, and the name of the value to delete. The handle to the key for most purposes will be one of the constant handles to a root key, and the path will be the path to the key. The path, as with the intrinsic VB functions can include the "" sign to signify sub keys. If you want to delete a value in the root of the key specified pass strPath as vbNullString. If you want to erase the default value, pass strValue as vbNullString.

Deleting Keys

Public Sub DeleteKey(ByVal hKey As Long, ByVal strPath As String)
Dim lRegResult As Long

lRegResult = RegDeleteKey(hKey, strPath)

End Sub

This function, as above, takes a handle to a key and a path to a key. It will find the key specified, and will delete all sub-keys and values, and using the 'drive' model would be equivalent to the 'deltree' DOS command - only without confirmation. Use this carefully, as it can have disastrous effects!

Creating Keys

Public Sub CreateKey(hKey As Long, strPath As String)
Dim hCurKey As Long
Dim lRegResult As Long

lRegResult = RegCreateKey(hKey, strPath, hCurKey)
If lRegResult <> ERROR_SUCCESS Then
' there is a problem
End If

lRegResult = RegCloseKey(hCurKey)

End Sub

Now that we are getting going, this does not need too much explaining. Pass a handle to a key and a path, and it will create all necessary keys to create the specified key. If you want to write an error handler into these functions, the function provides a space for checking whether the function has been a success.





Page 3 of 6



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel