| The Windows Registry: from the Ground UpEnumerating
Keys From the Windows Registry
Now that we have covered the basic aspects of Visual Basic and the Windows API, we now need to look at how to accomplish more advanced tasks, like enumerating keys from the Windows Registry, values from the Windows registry, backing up the registry and more. Enumeration of the Windows Registry can be quite tricky at start, but after you have read this article you will be able to create these functions in your sleep. The registry contains lots of sub-keys that make up the hierarchy of the Windows Registry. You may want to enumerate the keys in the registry, for example if you wanted to create your own registry editor, you would want to be able to enumerate the keys from the registry. In this section of the article, I will show you how you can enumerate all the sub-keys from the registry key: HKEY_LOCAL_MACHINE\SOFTWARE\ If you open up registry editor, by clicking on the start button and go the run prompt. Type in RegEdit and click on OK. Now open up the key shown above and you will something like the following.
You can see all the registry keys that they are, so how do me use Visual basic and the Windows API to enumerate these keys. This can be done using the following Windows API functions: RegOpenKeyEx RegEnumKeyEx RegCloseKey The RegOpenKey and RegCloseKey API functions have been explained in a previous section of this article. If you need to look back, then visit the contents page. The RegEnumKeyEx API function has the following parameters: hKey This is the opened key handle, that you can find using the RegOpenKeyEx API function (see earlier section for more information). dwIndex This is the index of the key that you want to enumerate. You would call this function increasing this parameter until know more data is available for enumerating. A common mistake that is often made is by the fact that if you ask a person what is the lowest positive number and they will tell you 1, but at all mathematicians will tell you, 0 is the number o start counting from. You should therefore start the first call of the function leaving this parameter as 0 and after each call increase the value by one. lpName This is the returned data containing the name of the key, which we have enumerated. This parameter also returns a null character, and must be passed by value (ByVal). You must fix the length of this string before calling this API function to MAX_PATH + 1 (255 + 1), as the Windows API uses fixed length strings. lpcbName This is the length of the returned key name, which you can use with the left function to resize the string. When passing this parameter, you must make it equal to MAX_PATH + 1 (255 + 1). After calling this parameter contains the length of the key. The rest of the parameters for this function are not needed by the Visual Basic programmer and should be left as null (0&). Now how can you use the function to enumerate keys? First you need to open the key that you wish to enumerate using the RegOpenKeyEx API function. You would then need to set the counter to 0 for the first key, and then use a do loop to keep enumerating while there is data is available. When there is no more data available, you need to exit the do and close the registry key. Now for the sample. Create a new standard-EXE project in Visual basic and add a command button, list box to form1. Set the name of the command button to cmdEnumKey and the caption to Enum Keys. Now add the following code to form1s code module: Option Explicit Private Sub cmdEnumKey_Click() Dim lKeyHandle As Long, sTemp As String _ , sTempLen As Long, lTemp As Long, _ lIndex As Long ' Declare variables to hold opened key ' handle and error information lIndex = 0 lTemp = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE", _ 0&, KEY_ALL_ACCESS, lKeyHandle) Do sTempLen = 255 + 1 ' Set the length that we want to use to size ' the string sTemp = String(sTempLen, 0) ' Size the string lTemp = RegEnumKeyEx(lKeyHandle, lIndex, ByVal sTemp, sTempLen, 0&, 0&, 0&, 0&) ' Call the Enum function to return information If lTemp = REG_NOMOREDATA Then Exit Do ' The Enum function will either return: ' More Data Available or ' No More Data available ' If there is no more data available then we want ' to end the loop lstKeys.AddItem Left$(sTemp, sTempLen) ' Add the key to the list box after resizing it. ' NB: Re resize the string to remove the null ' character lIndex = lIndex + 1 ' Increase the index count Loops Call RegCloseKey(lKeyHandle) ' close the opened key by passing the key handle sTemp = String(100, 0) ' Fix the length of the string sTempLen = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM _ Or FORMAT_MESSAGE_IGNORE_INSERTS, vbNullString, lTemp, _ 0&, sTemp, Len(sTemp), ByVal 0&) ' Call the format message API function to return ' error information in the form of a string MsgBox Left$(sTemp, sTempLen) ' Use the length of the error string returned ' to reduce the string back down to size and ' display the message in a message box End Sub If you now add the required declarations and run the project, click on the command button and the keys from the registry will be displayed in the list box. For a full sample and more detailed comments, you can download the sample, by clicking on the link below. In the next section of this article, learn how to enumerate values from the Windows registry. |
___________________________________________ Send Bugs, comments to the webmaster at webmaster@mwe.8m.com © 1999 MW Software, all rights reserved |