Venky's World

     

    Using the registry with Visual Basic-I

    (Creating keys and storing values)

     

    The registry is a very important part of the Windows operating system. The registry can be thought of as a place where windows and other application keep important information which is needed by them. This information could be about specific hardware or software configuration. The registry gives us immense power to store and retrieve data from it. But with power comes responsibility. A small error in the registry could bring the windows OS crashing down.

    Before we get into programming/using the registry let's take a brief look at the structure of the registry.

     .

     The registry structure is similar to a tree structure. The 6 main branches in the registry are called "HIVES". These branches or "HIVES" are as follows

     

    1. HKEY_CLASSES_ROOT - This HIVE contains all information about shortcuts and file association types.
    2. HKEY_CURRENT_USER - This HIVE is linked to the HKEY_USERS section which has information about the current user who has logged in.
    3. HKEY_LOCAL_MACHINE - This HIVE basically contains information about the kind of hardware and software that has been installed in the machine.
    4. HKEY_USERS - This HIVE contains specific information about the preferences of each and every user of the computer.
    5. HKEY_CURRENT_CONFIG - This HIVE is linked to the HKEY_LOCAL_MACHINE section having data for the current hardware configuration.
    6. HKEY_DYN_DATA - This HIVE points to that part of HKEY_LOCAL_MACHINE which is used for the Plug-&-Play features of Windows, this section is continuously updated as and when devices are added and removed from the system.

    The hives are made up of KEYS. These keys can have SUBKEYS as well as Values. The values contain the actual information in the registry.

    Looking at the various HIVES we can come to a conclusion that the HIVES which are apt for creating KEYS and storing values for our programs are HKEY_CURRENT_USER & HKEY_LOCAL_MACHINE.

    Let us now look at creating keys and storing values. The Api's which we shall use are as follows

    Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

    Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

    Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData 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
     

    and also the following constants

    Private Const HKEY_CURRENT_USER = &H80000001

    Private Const HKEY_LOCAL_MACHINE = &H80000002

    Now let's create a key in HKEY_LOCAL_MACHINE.The code we shall use for doing this is as follows

    Dim result As Long
    Dim pres As Long
    result = RegCreateKey(HKEY_LOCAL_MACHINE, "Software\RegDemo", pres)

     

    The first parameter in the RegCreateKey function is the main KEY or HIVE under which we want to create a subkey to input values. In this particular case the parameter is the hive HKEY_LOCAL_MACHINE. The second parameter is the subkey which we want to create. In this case we want to create a subkey RegDemo under 'Software' Key. The third parameter will contain the 'handle', a unique identifier for the key which was created .This parameter is filled with the value of the handle if the function was executed succesfully.

    Here we have  created a new Key called RegDemo which is the name of the sample application under "HKEY_LOCAL_MACHINE\Software". Now we can put in all the data under the RegDemo Key. Now we shall put in some values under this key. This we shall do by using RegSetValue. Let us assume we have this application where we have to save the name of the person who last used it ,date when the application was last used and the time when it was used. Before we can set a value in a key we need to open that key using RegOpenKey.

     

    Dim result As Long
    Dim keyres As Long
    result = RegOpenKey(HKEY_LOCAL_MACHINE, "Software\RegDemo", keyres)

    The parameter in RegOpenKey are similar to the parameter of RegCreateKey

    After executing this statement we will get a 'handle' to the key which we have opened.This handle is stored in keyres. After we have opened this key let us put in a name as a value 

    Dim entry As String
    entry = "Name"
    result = RegSetValueEx (keyres, entry, 0,0, ByVal txtName.Text, Len(txtName.Text))

    The first parameter in RegSetValueEx function is the handle to the key in which values have to be stored. We get this handle by using the RegOpenKey function.The second parameter is the value name in this case it is "Name",the 3 parameter is reserved and should always be 0.The 4th parameter is also 0.The 5th parameter is the value of second parameter in this case it is a variable returned by txtName.text.The 6th parameter is the length of the value or the 5th parameter.

    Here txtName.text is a value entered by the user.

    Let us also put in the current date

    Dim entry As String
    entry = "Date"
    Dim date1 As String
    date1 = Date
    result = RegSetValueEx (keyres, entry, 0,0, ByVal date1, Len(date1))

     and the current time

    Dim entry As String
    entry = "Time"
    Dim time1 As String
    time1 = Time
    result = RegSetValueEx(keyres, entry, 0, 0, ByVal time1, Len(time1))

     

    In order to read values from the registry we need to use the RegQueryValueEx  Api function.If we have to read the values that we have put in the registry we do it in the following way.We first open the key whose value is to be read.

    Dim result As Long
    result = RegOpenKey(HKEY_LOCAL_MACHINE, "Software\RegDemo", keyres)


     

    After opening the key we get it's handle in keyres. Now we use the RegQueryValueEx  function in the following way

    entry = "Name"
    result = RegQueryValueEx(keyres, entry, 0, 0, "", length)
    val1 = Space$(length - 1)
    result = RegQueryValueEx(keyres, entry, 0, 0, ByVal val1, length)
     

    The first parameter in the RegQueryValueEx function is the handle to the key to be opened for reading ,in this case keyres,2nd parameter is the value name to be read here it is "Name". The 3rd parameter is reserved and should be 0.The 4th parameter is also 0.The 5th parameter is a pointer to a string variable which will hold the value of the key after the function  is succesfully executed. The 6th parameter is the length of the value.Notice that we have used the RegQueryValueEx function twice. The first time we use it to get the length of the value .After We get the length ,we fill the string variable with empty space in order to hols the result.After that we call RegQueryValueEx again and this time after successfull execution val1 will conatin the value of the key.

    Similarly we can get the values of "Date" 

    entry = "Date"
    result = RegQueryValueEx(keyres, entry, 0, 0, "", length)
    val2 = Space$(length - 1)
    result = RegQueryValueEx(keyres, entry, 0, 0, ByVal val1, length)

    & "Time"

    entry = "Time"
    result = RegQueryValueEx(keyres, entry, 0, 0, "", length)
    val2 = Space$(length - 1)
    result = RegQueryValueEx(keyres, entry, 0, 0, ByVal val1, length)