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

Opening Keys

As each object in windows has a unique handle to allow it to be distinguished from each other, the same applies with registry keys. When you want to open up a registry sub key you must open the key and retrieve its handle before you can do anything else.

If looked at the table in the previous section then you will see that there is a RegOpenKey and a RegOpenKeyEx. The RegOpenKey was used for compatibility with 16-bit windows wih Visual Basic 4, but now as we are currently on version 6 of Visual Basic the RegOpenKeyEx API function should always be used on the 32-bit operating system. If you use the API viewer and you copy the declare function your Visual Project you will find that it looks like the following:

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

When you first come across a Windows API function it is best to break down its parameters as I have done below and you will find an explanation of each:

Hkey – This the registry key that you want to open, which is the same as you can view from the Registry editor. When passing these is Visual basic you need to pass the keys a hexadecimal, which you can choose from the following

Registry Key Constant
HKEY_CLASSES_ROOT &H80000000
HKEY_CURRENT_USER &H80000001
HKEY_LOCAL_MACHINE &H80000002
HKEY_USERS &H80000003
HKEY_PERFORMANCE_DATA NB: Windows NT Only &H80000004
HKEY_CURRENT_CONFIG &H80000005
HKEY_DYN_DATA &H80000006

lpSubKey – This is the sub-key that you wanted to open, for example

HKEY_LOCAL_MACHINE\SOFTWARE

Would be SOFTWARE as you would have already passed HKEY_LOCAL_MACHINE in the Hkey parameter.

ulOptions – This parameter is used by Windows and therefore is reserved and must be left as null (0&)

samDesired – This parameter specifies the type of access that you would like when accessing the registry. You could want only to have read access to the registry. I found that these seemed to have little use on Windows 95, so are therefore only useful with Windows NT. You therefore only really need to use the KEY_ALL_ACCESS to the registry. Below is a table summarising the access that can be used:

Constant Explanation
KEY_ALL_ACCESS This constant grants full access to the registry on Windows 95. On Windows NT, there will be parts of the registry that can only be accessed by members of the Administrators group or other security that has been set.
KEY_CREATE_LINK This constant allows you to create a symbolic link in the system registry. NB: You do not need to worry about this for now.
KEY_CREATE_SUB_KEY This constant allows you to create sub-keys in the registry.
KEY_ENUMERATE_SUB_KEYS This constant allows you to enumerate sub keys in the registry.
KEY_EXECUTE This constant allows read access to the registry, but will not allow enumerating of keys.
KEY_NOTIFY This constant allows you to change data in the registry.
KEY_QUERY_VALUE This constant allows you to retrieve information about values of sub keys in the system registry.
KEY_READ This constant allows full access to the registry, but you cannot change any part of the registry.
KEY_SET_VALUE This constant allows you to write to the values of keys in the system registry.
KEY_WRITE This constant allows full write access to the system registry.

For general purposes though I strongly suggest that you use KEY_ALL_ACCESS

phkResult – This points to a variable that you have declared that will return the handle of the key that you have just opened.

If the function fails for some reason, say you specified an invalid sub key then the return value of the function will be returned. A better way of getting error information for the registry API’s is to use the FormatMessage API function, which will return error information in the form of a string. An example of this will be shown below in the following example, which demonstrates opening a key. Before this example you need to realise that when you have opened a key, you must close it. If you do not you will run in all sorts of problems. One problem I found when I first started experimenting with the registry several years ago was that if you did not close the key that you had opened, you would find that you could not delete any information contained in that key as well as modify it, even using the Registry Editor.

The RegCloseKey API function has one parameter, which you pass the value returned to the phkResult parameter in the RegOpenKeyEx API function.

An example of how to do this is displayed below:

Create a new standard-EXE project and add a command button to the form. Set its name property to cmdOpenKey and its caption property to Open Key. Add the following code to the code window 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 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 cmdOpenKey_Click()

Dim lKeyHandle As Long, lTemp As Long, _

sTemp As String, sTempLen As Long

' Declare variables to hold opened key

' handle and error information

lTemp = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE", _

0&, KEY_ALL_ACCESS, lKeyHandle)

' Call the RegOpenKeyEx function to open up

' HKKEY_LOCAL_MACHINE\SOFTWARE and return the key

' KeyHandle to the lKeyHandle variable

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

Run the project and click on the command button. If the opening of the key was successful then The operation completed successfully will be displayed in the message box, otherwise the error that occurred will be displayed in the message box. Errors that occur are mainly if the sub-key that you specified does not exist. In that case the message The file specified was not found will be displayed.

If you have read through the comments clearly then you should understand how this works. In the next section you will learn how to use the API to retrieve values from the registry.

You can also download this sample by clicking on the link below:

Download

<< Back                                                                                   Next >>

___________________________________________

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

Terms of Use

© 1999 MW Software, all rights reserved