Accessing the Windows Registry with the API
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.
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!
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