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

Storing Information In the Windows Registry

Now that you have learned how to retrieve a string value from the registry, you will also want to know how do you write information to the registry.

This can be done by using the RegSetValueEx, RegCreateKeyEx and the RegCloseKey API functions.

The RegSetValueEx stores information in the registry and in this section I will show you how to store a string value. For other data-types you will examples of this in my structured registry class at the end of this article. One confusing part of using these API’s is that you don’t use the RegOpenKeyEx API function. Instead you must use the RegCreateKeyEx API function.

The RegCreateKeyEx API function as the following parameters

hKey – This is the registry key that you want to open(See Above)

lpSubKey – This is the sub-key that you want to open.

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

lpClass – Leave this as null (0&)

dwOptions – This sets options when creating the new key. On Windows 95 use the constant REG_OPTION_NON_VOLATILE, but on Windows NT you have many other options. The constants that can be used are

REG_OPTION_NON_VOLATILE The value stored is non volatile, which means it will permanently remain in the registry.
REG_OPTION_VOLATILE On Windows NT, you can use this constant to store data is volatile data, which means that the data is stored in memory and on powerful off the data is lost.
REG_OPTION_BACKUP_RESTORE As a Visual Basic developer, this is not necessary.

samDesired – This is the same as RegOpenKeyEx and is the access you require. On Windows 95/98 leave this as KEY_ALL_ACCESS.

lpSecurityAttributes – This is not needed at the moment and will be explained in a later article. Therefore leave this as null (0&).

phkResult – This is the same as RegOpenKeyEx, where the handle of the newly created key is returned to a variable that you specify.

lpdwDisposition – This parameter returns data to a specified variable, which can either of the following:

REG_CREATED_NEW_KEY The sub-key that you specified doesn’t exist and therefore the API function creates the new sub-key.
REG_OPENED_EXISTING_KEY The sub-key that you specified already exists, so the key is just opened and the handle is returned. No data is overwritten.

The return vales are the same as the other registry API functions.

The RegSetValueEx API function has the following parameters.

hKey – This is the handle that was returned by the RegOpenKeyEx API function.

lpValueName – As the sub-key was opened with the RegOpenKeyEx API function you now need to specify the key that you want to query its value for.

If you wanted to save a value to HKEY_LOCAL_MACHINE\SOFTWARE\MW-SOFTWARE\ with the key being Rating then you would use Rating as the value name.

Reserved – This is used by windows and therefore should be left null (0&)

dwType – This is the type of value that you want to store in the registry. In this example we will use the REG_SZ constant, but in practical use you can use other data-types, but you will need to view the structured class at the end of this module to be able to do this.

lpData – This is the data that you want to store in the registry. This bust be passed by value.

cbData – This is length in bytes of the data that you have passed to the lpData parameter. You use the Len built in function of visual basic to calculate this, but remember to include the null-terminating character.

The return value is the same as the previously shown registry API functions.

Below you will find a sample, which saves a string value to:

HKEY_LOCAL_MACHINE\SOFTWARE\MW-SOFTWARE

Start a new standard-EXE project and add a command button to form1. Change the command buttons name to cmdSaveValue and the caption to Save Value. Now add the following code to form1’s code window:

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 RegCreateKeyEx Lib "advapi32.dll" _

Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey _

As String, ByVal Reserved As Long, ByVal lpClass As String, _

ByVal dwOptions As Long, ByVal samDesired As Long, _

lpSecurityAttributes As Any, phkResult _

As Long, lpdwDisposition 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 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 Const REG_OPTION_NON_VOLATILE = 0

Private Const REG_SZ = 1

Private Const REG_CREATED_NEW_KEY = &H1

Private Const REG_OPENED_EXISTING_KEY = &H2

Private Sub cmdSaveValue_Click()

Dim lKeyHandle As Long, sTemp As String _

, sTempLen As Long, lTempT As Long, lTemp As Long

' Declare variables to hold opened key

' handle and error information

sTemp = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\MW-SOFTWARE" _

, 0&, 0&, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0&, _

lKeyHandle, lTempT)

' Create the MW-SOFTWARE sub-key and return the handle

' to the lKeyHandle variable

If lTempT = REG_OPENED_EXISTING_KEY Then

MsgBox "The sub-key already exists in the Windows " & _

"Registry. The sub-key will not be overwritten"

End If

' Check if the key already exists and warn the user, that the

' existing key will not be overwritten.

sTemp = RegSetValueEx(lKeyHandle, "Rating", 0&, REG_SZ, ByVal _

"10", Len("10"))

' Create a new key called Rating and store the value 10

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 run the project and click on the command button. If the key doesn’t exist then it will be created, and if the key does exist then the user will be warned that data will not be overwritten.

For examples of storing different data-types see the structured class at the end of this article.

You can also download a ready-done sample by clicking on the link below

Download

In the previous sample the code to create a new sub-key in the registry was shown. I am going to leave you to extract the code and make your own function.

<< Back                                                                                   Next >>

___________________________________________

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

Terms of Use

© 1999 MW Software, all rights reserved