Free Web Hosting Provider - Web Hosting - E-commerce - High Speed Internet - Free Web Page
Search the Web

MW-Menu

 

Free Visual BASIC Newsletter

Features

Tips

Sample Code

Newsletters

Products

ActiveX

Cool Links

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The Windows Registry: from the Ground Up

Enumerating Values From the Windows Registry

Now we move to the second of out more advanced topics, which will proves most interest to Visual Basic programmers and that is enumerating values from the Windows Registry.

This can be done in the same way as enumerating keys from the registry, but instead using the RegEnumValue API function, which has the following parameters:

hKey – This is the handle to the key that you have opened using the RegOpenKey API function. For more information see the section on opening keys.

dwIndex – This is the index of the value that you want to enumerate. Start this at zero and increase it until there is no more data available to enumerate.

lpValueName – This is the value key name that has been enumerated. You could use this value key name along with the RegQueryValueEx API function to return the value, but this function will also return the value. There may be cases though, where you will want to use the value key name and a separate function for example if the values are of different types. The example in this section of the article, will only return values, which are a string data-type. You must also remember to pass this parameter by value (ByVal) or the function will fail. You also need to size this string to MAX_PATH + 1 (255 + 1) or the function will fail.

lpcbValueName – This the size of the returned value name. NB: You need make this parameter the size of the lpValueName parameter. You can then use this value to resize the string afterwards.

lpReserved – This parameter is used by windows and therefore should be left as null (0&).

lpType – This parameter will return the type of the value that is contained in the key value. You will find that this will normally be either REG_SZ, REG_BINARY or REG_DWORD. You will only need this for returning values of a certain type. I would suggest for other uses to have a separate function for returning the values of different data-types. If you don’t need this then you can leave it as null.

lpData – This is the value of the value key name, and can be of different data-types, which is determined by the lpType parameter. This also must be sized to MAX_PATH + 1 and must be passed by value.

lpcbData – This is the size of the value, and must be MAX_PATH + 1. The returned function will return the length of the string, so you can use this value to resize the string to remove the null character.

You use the function in the same way as the RegEnumKeyEx API function, but taking into account the extra parameters for the value data of the value key name.

You may want to try to write a function to do this your self using the technique explained in the previous article. If not then take a look below to learn how it is done:

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 cmdEnumValues and the caption to Enum Values.

Now add the following code to form1’s code module:

Option Explicit

Private Sub cmdEnumKey_Click()

Dim lKeyHandle As Long, sTemp As String _

, sTempLen As Long, lTemp As Long, _

lIndex As Long, sTempData As String, sTempDataLen _

As Long, sTempType As Long

' Declare variables to hold opened key

' handle and error information

lIndex = 0

lTemp = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion", _

0&, KEY_ALL_ACCESS, lKeyHandle)

Do

sTemp = String(255 + 1, 0)

' Size the strings

sTempData = String(255 + 1, 0)

sTempLen = 255 + 1

sTempDataLen = 255 + 1

lTemp = RegEnumValue(lKeyHandle, lIndex, ByVal sTemp, sTempLen, 0&, sTempType, ByVal sTempData, sTempDataLen)

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

If sTempType = REG_SZ And (Left$(sTempData, sTempDataLen)) <> "" Then

' Only add string values to the list box

lstValues.AddItem Left$(sTemp, sTempLen) & " = " & _

Left$(sTempData, sTempDataLen)

End If

lIndex = lIndex + 1

' Increase the index count

Loop

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 run the program, you will see that all the value key names and the contained values are returned and displayed in the list box.

You can download a full sample, with full detailed comments by clicking on the following link:

Download

Following on next week will be two sections following on from here on backing up and restoring parts of the registry as well as gaining information on information in the registry as well as extra functions that are available of Windows NT.

If you have any queries, bugs, comments about any of the code in this article then do not hesitate to contact me on:

Steve@mwe.8m.com

<< Back                                                                                   Next >>

___________________________________________

Send Bugs, comments to the webmaster at webmaster@mwe.8m.com

Terms of Use

© 1999 MW Software, all rights reserved