The Windows registry is the central database of any Windows PC. In this article Neville explains the registry, and shows us how to access it using both Visual Basic's functions and the Windows API.
Registry Fever With Visual Basic - Using the Windows API To Access the Registry (Page 4 of 5 )
I'll assume that you are aware of what the Windows API is and how we use it in Visual Basic.
Whenever we are going to use any API function, we need to declare it in our code. In our case, we would have to declare the following functions:
Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Public Declare Function RegCloseKey Lib "advapi32.dll" Alias "RegCloseKey" (ByVal hKey As Long) As Long
Creating a Key Lets create a function that uses the RegCreateKey API function internally to create a key anywhere in the registry:
Private Sub CreateNewKey() Dim hNewKey As Long Dim lRetVal As Long
lRetVal = RegCreateKey(HKEY_CURRENT_USER,"TestKey", hNewKey) RegCloseKey (hNewKey) End Sub
Let's look at what this code does.
First up, we call the RegCreateKey function and pass 3 parameters to it. Firstly, the hive in which we want to create the key. Secondly, the name of the key that we want to create under this hive, and finally, the key handle, which can be an empty long variable.
After this line is executed, the hNewKey long variable would contain a reference to the newly created key. This line would create a key right under the HKEY_CURRENT_USER hive. The successful execution of this line would return a long value, which we trap in the lRetVal variable.
Next up, we call the RegCloseKey function to close the reference that we have just created. This is done in order to free CPU resources. It's not a mandatory step, but should be done. It takes just one parameter, which is the key handle (or object) to be closed.
Deleting a Key Let's code a function that uses the RegCreateKey API function internally to delete a key anywhere in the registry:
Private Sub Form_Load() Dim lRetVal As Long Dim hNewKey As Long
Firstly, we call the RegOpenKey function and pass 3 parameters to it: the hive in which we want to delete the key, the key path to delete, and the key handle.
After this line is executed, the hNewKey long variable would contain a reference to the key path specified in the RegOpenKey function. This line would create a reference to the HKEY_CURRENT_USER hive. The successful execution of this line would return a long value, which we trap in the lRetVal variable.
Secondly, we call the RegDeleteKey function and pass 2 parameters to it. Firstly, the reference to the hive we need to delete the key from. Secondly, the key path to delete. This line would delete the "Test" key under the HKEY_CURRENT_USER hive. It would also end up deleting all of its sub keys and values too. The successful execution of this line would return a long value, which we trap in the lRetVal variable.
Finally, we call the RegCloseKey function to close the reference we have just created. This is done in order to free CPU resources. This is not a mandatory step, but it should be done. It takes one parameter, which is the key handle to be closed.