| The Windows Registry: from the Ground UpRetrieve
Information from the Windows Registry
Now that you have learned how to open a key in the registry, it is now time to learn how to implement the most useful features of the registry into your applications. The APIs required for this are: RegQueryValueEx RegSetValueEx RegCreateKey RegDeleteKey RegDeleteValue In this section I will go through each API individually, with a detailed sample for each. First the most important API, retrieving information from the Windows Registry. This requires the RegOpenKeyEx, RegQueryValueEx and the RegCloseKey API functions. One of the biggest mistakes that is made here is just using the RegQueryValueEx API function, but you will find that this will not work as you have to open the key first and return its handle. The RegQueryValueEx API function as the following parameters: hKey This is the handle that was returned by the RegOpenKeyEx API function.
If you were to open up the registry editor to: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion and wanted to find out who the registered owner windows was then you would want to query the key RegisteredOwner.
lpType This points to a variable that will hold the type of value that is contained, with the most popular being a string value. Other possible data-types are listed in the table below
lpData This points to a variable to contain the data that is stored in the registry. For more use and as shown in the structured class at the end of this article the use the variant data-type is more practical and efficient to use. lpcbData This points to a variable to hold the number of characters in the value(s) returned. This is required for resizing the value depending on the data-type. This does include the null-terminating character, so after the value as been retrieved you need to increase the value by one to take into the account the null-terminating character. The return values will be the same as for the RegOpenKeyEx API function and the FormatMessage API function can be used to get an error message. Now for an example of how to use the RegQueryValueEx API function to return a string value from the registry. As the windows API is assumed to be written in C, you will need to pass a fixed length string. In order to do this you must call the API function RegQueryValueEx with the lpData left as null (remember to by the null by value) and use the length of the value returned to the lpcbData variable to size the string using the String function, which is built into Visual Basic. You can then call the API function again with the fixed length string to return the value. To resize the string you can use the left command, but remembering to take into account that a null-terminating character is returned to the lpData variable as well. Below is a sample showing you show to return the registered owner of Windows from the registry: Start a new standard-EXE project, add a command button to form1. Change the command buttons name to cmdGetValue and its caption to GetValue. Insert the following code into the code module of form1: Option Explicit Private Declare Function FormatMessage Lib "kernel32" Alias _ "FormatMessageA" (ByVal dwFlags As Long, lpSource As _ Any, ByVal dwMessageId As Long, ByVal dwLanguageId As _ Long, ByVal lpBuffer As String, ByVal nSize As Long, _ Arguments As Long) As Long Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000 Private Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200 Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias _ "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _ ByVal ulOptions As Long, ByVal samDesired As Long, phkResult _ As Long) As Long Private Declare Function RegCloseKey Lib "advapi32.dll" _ (ByVal hKey As Long) As Long Private Declare Function RegQueryValueEx Lib "advapi32.dll" _ Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName _ As String, ByVal lpReserved As Long, lpType As Long, lpData As _ Any, lpcbData As Long) As Long Private Const STANDARD_RIGHTS_ALL = &H1F0000 Private Const KEY_QUERY_VALUE = &H1 Private Const KEY_SET_VALUE = &H2 Private Const KEY_CREATE_SUB_KEY = &H4 Private Const KEY_ENUMERATE_SUB_KEYS = &H8 Private Const KEY_NOTIFY = &H10 Private Const SYNCHRONIZE = &H100000 Private Const KEY_CREATE_LINK = &H20 Private Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or _ KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY _ Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or _ KEY_CREATE_LINK) And (Not SYNCHRONIZE)) Private Const HKEY_LOCAL_MACHINE = &H80000002 Private Sub cmdgetvalue_Click() Dim lKeyHandle As Long, lTemp As Long, _ sTemp As String, sTempLen As Long, _ sData As String, sDataLen As Long ' Declare variables to hold opened key ' handle and error information, etc... lTemp = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _ "SOFTWARE\Microsoft\Windows\CurrentVersion", _ 0&, KEY_ALL_ACCESS, lKeyHandle) ' Call the RegOpenKeyEx function to open up ' HKEY_LOCAL_MACHINE\SOFTWARE and return the key ' KeyHandle to the lKeyHandle variable lTemp = RegQueryValueEx(lKeyHandle, "RegisteredOwner" _ , 0&, 0&, ByVal 0&, sDataLen) ' Query the value to find the length of the value ' NB: As we are querying a string value you do not ' need to return the data-type so leave it null sData = String(sDataLen, " ") ' Fix the length of the string to the length of the value lTemp = RegQueryValueEx(lKeyHandle, "RegisteredOwner", _ 0&, 0&, ByVal sData, sDataLen) ' Call the API function to return the value of the ' RegisteredUser key to the sData variable MsgBox Left$(sData, sDataLen + 1) ' Resize the string using the string length and ' taking into account the null-character 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 project and click on the command button then the registered user of windows is returned. If the function was successful then the message The operation completed successfully will be displayed in a message box. Make sure that you read through the detailed comments to makes sure that you understand how this works. When retrieving values from the registry, there may be different data-types. Some of these are quite rare, but are all shown in the structured registry class at the end of this article. You can also download a ready made version of this project by clicking on the link below |
||||||||||||||||||||||
___________________________________________ Send Bugs, comments to the webmaster at webmaster@mwe.8m.com © 1999 MW Software, all rights reserved |