Skip to main content

Past Blast

Featured Products

Stay in touch using the DEVBUSS RSS feeds.
 

News

Edit Pocket PC Windows CE Registry using eVB (eMbedded Visual Basic) and Windows CE API calls.

Written by David Bailey  [author's bio]  [read 73089 times]
Edited by Derek

Page 1  Page 2 

The Functions

CEGetSetting and CESaveSetting are similar. For brevity, I will discuss CEGetSetting here. Both functions are contained in modReg.bas.

Definition and Declarations

Everything in eVB is a variant but the IDE still allows us to declare variables and parameters as we would in VB6. I have found that declaring them with their proper types does not affect the application but does provide IntelliSense support.

Key_Read is created from the constants shown above.

Public Function CEGetSetting(ByRef strError As Variant, ByVal APPNAME As String, _ ByVal Section As String, ByVal Key As String, ByRef Setting As String) As Boolean

On Error Resume Next

Dim lngSize As Long
Dim hlngSubKey As Long
Dim lngType As Long
Dim lngResult As Long
Dim Key_Read As Long
Dim SubKey As String

'False until proven true...
CEGetSetting = False
Key_Read = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS OrKEY_NOTIFY) And (Not SYNCHRONIZE))

Without an On error goto construct, error handling in eVB has to be done a little differently. As discussed above I declare every function with a boolean return value and a (ByRef) error string. I set the return value false for safety at the top of the routine. If the routine executes to completion the return value will be set to true. If an error occurs anywhere in the function the return value will remain false.

Validate Parameters

The API calls are not at all forgiving. It is important to validate the incoming parameters before making the API calls. If you thought debugging API calls in VB 6 was no fun just wait

'Fill the "Setting" parameter with 256 chr(0)'s
lngSize = 256
Setting = String(lngSize, 0)
SubKey = APPNAME & "\" & Section

The SubKey is the concatenation of the application name and section. Recall that in our example the application name is SOFTWARE\pktSoft\TTDS and the section is Settings so the SubKey becomes SOFTWARE\pktSoft\TTDS\ Settings.

Open the Registry

We are ready to make the first API call. We want to try and open the key under HKEY_LOCAL_MACHINE. If we are able to open the key then lngResult will be 0 and hlngSubKey will be set.

'Open the registry key and find a handle to the sub key
'It is always under HKEY_LOCAL_MACHINE
'Return hlngSubKey, lngResult indicates success or failure

lngResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, SubKey, 0, Key_Read, hlngSubKey)

If (lngResult <> ERROR_SUCCESS) Then
  strError = "CEGetSetting: Unable to open the registry sub-key '" & SubKey & "'."
  Exit Function
End If

Try to Read the Key and get the Setting

If we made it this far then we should be able to open the Key and read the Setting. The function uses hlngSubKey from the previous call.

'Try to read the value of the item
'Pass in hlngSubKey and the Key we are looking for.
'Setting is 256 chr(0) on the way in (Byref).
'Return lngType, strItemValue, lngSize
lngResult = RegQueryValueEx(hlngSubKey, Key, 0, lngType, Setting, lngSize)
If (lngResult = ERROR_SUCCESS) And (lngType = 1) Then
  'OK - the Setting came back but it is 256 chars long, padded with chr(0).
  Setting = Left(Setting, lngSize - 1)
Else
  'Error...
  'Close the registry before we exit
  lngResult = RegCloseKey(hlngSubKey)
  lngResult = RegCloseKey(HKEY_LOCAL_MACHINE)
  strError = "CEGetSetting: Unable to read the registry value for '" & Key & "'. (" & lngResult & ")."
  Exit Function
End If

Clean Up

If we got this far then we have successfully retrieved the setting. We can close the registry and set the CEGetSetting return value to True.

'All is well if we got this far. Close the keys.
lngResult = RegCloseKey(hlngSubKey)
lngResult = RegCloseKey(HKEY_LOCAL_MACHINE)
strError = ""
CEGetSetting = True

Sample Calls

I normally read registry keys when the application loads (in the form_load event) and save registry settings when a form or the application closes. This code executes so fast that you can save settings in text_change events if needed. Assume we want to retrieve the name of the SQL Server 2000/CE database name from the registry when the application is first run.

APPNAME is globally declared as follows:

Public Const APPNAME = "SOFTWARE\pktSoft\TTDS"

The following code is from the form_load event:

Dim bRes As Boolean
Dim strError As String
Dim strDB as String

bRes = CEGetSetting(strError, APPNAME, "Settings", _
"DatabasePath", strDB)

If bRes = False Then
  MsgBox strError, vbExclamation
Else
  txtLocalDatabase.Text = strDB
End If

If the setting is found then the text control in the application is set. When this application closes the DatabasePath is written back to the registry (in case the user changed it). Note that we can pass the control text property as a function parameter. The code (in the Form_OKClick event) is:

Dim bRes As Boolean
Dim strError As String

'Save the database path
bRes = CESaveSetting(strError, APPNAME, "Settings", _
"DatabasePath", txtLocalDatabase.Text)
If bRes = False Then
  MsgBox strError, vbExclamation
End If

Summary

As VB 6 developers we have come to rely on the Windows API to implement functionality not found in the base language. Windows CE offers many familiar API functions and eVB can use them as easily as VB 6 uses the Windows API. Convenient access to the registry is something I rely on in every VB 6 application I write and porting my existing code to eVB was relatively easy.

[1] HKEY_LOCAL_MACHINE is added in code

Previous Page