Skip to main content

Past Blast

Featured Products

Windows Mobile Developer Controls
Windows Mobile Developer Controls
Stay in touch using the DEVBUSS RSS feeds.
 

News

Windows Mobile Developer Controls
Windows Mobile Developer Controls

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 

All modern desktop applications save and retrieve settings using the Windows registry. Storing settings in the registry saves the user the trouble of re-entering application settings like database paths etc. In VB 5 and later we have two functions (GetSetting and SaveSetting) that let us easily save settings to the registry. A significant drawback with these functions is that they save the setting under the HKEY_CURRENT_USER hive. When a different user logs onto the PC they need to establish their own settings. The only way around this is to use the Windows API to create your own functions to get and save settings under the HKEY_LOCAL_MACHINE hive. Windows CE for the PocketPC also contains a registry and it can be used in much the same way as its desktop cousin. The Windows CE API is also very similar to its desktop counterpart so I was able to port my VB 6 registry code to eVB without too much trouble. This article discusses two functions I wrote and how you can easily use them in your application to save and retrieve settings from the Windows CE registry.

The above screen shot shows the PocketPC registry key (\pktSoft\TTDS) maintained for an application I have developed. The registry editor shown in this screen shot is the remote tool accessible from the eVB IDE.

Design

The GetSetting and SaveSetting functions in VB 6 are familiar to many developers so I wanted to mimic those two functions in terms of the parameter lists. Recall that in VB 6 the 2 function calls are:

SaveSetting (AppName as String, Section as String, Key a String, Setting as String)
strResult = GetSetting(AppName as String, Section as String, Key as String, Default as String)

I designed the two eVB functions as follows:

CESaveSetting(strError As String, ByVal AppName As String, ByVal Section As String, ByVal Key As String, ByVal Setting As String) As Boolean
CEGetSetting(ByRef strError As Variant, ByVal AppName As String, ByVal Section As String, ByVal Key As String, ByRef Setting As String) As Boolean

The departures from the intrinsic VB functions are as follows:

Boolean Return

All my functions and methods always return a boolean to the caller indicating success or fail. Return values are returned as ByRef parameters

StrError

All my methods and functions return an error string as the first parameter. If the function return value is True then this string will be empty. If the function return value is false then this string will contain the error message.

GetSetting default

I decided not to implement a default value for CEGetSetting. If the setting comes back empty your code can take appropriate actions to set a default.

Hive

I always want to save and get settings from HKEY_LOCAL_MACHINE, not HKEY_CURRENT_USER. HKEY_CURRENT_USER does not exist in the CE registry.

In the registry editor example screen shot above, the function parameters can be seen as:

CEGetSetting Parameter Name Value (as shown in screen shot)
AppName[1] SOFTWARE\pktSoft\TTDS
Section Settings
Key DatabasePath
Setting Ce

The Windows CE API eVB contains a great little registry editor that allows you to view the local (PC Windows) registry, the PocketPC emulator registry and the live PocketPC device registry (via ActiveSync) all at the same time. Interestingly the eVB emulator will only allow you to read emulator registry settings but will not allow you to write them. As a result you need to debug and test registry writing code with the Run on Target set to Pocket PC (Default Device). eVB also contains the standard API Viewer application. This utility gives us the correctly formatted API calls for eVB.

For the registry module we will use 5 API calls:

Public Declare Function RegOpenKeyEx Lib "Coredll" Alias "RegOpenKeyExW" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long

Public Declare Function RegQueryValueEx Lib "Coredll" Alias "RegQueryValueExW" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long

Public Declare Function RegCloseKey Lib "Coredll" (ByVal hKey As Long) As Long

Public Declare Function RegCreateKeyEx Lib "Coredll" Alias "RegCreateKeyExW" (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 Long, phkResult As Long, lpdwDisposition As Long) As Long

Public Declare Function RegSetValueEx Lib "Coredll" Alias "RegSetValueExW" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long

I also copied some useful constants from the API Viewer:

Public Const SYNCHRONIZE = &H100000
Public Const KEY_NOTIFY = &H10
Public Const READ_CONTROL = &H20000
Public Const STANDARD_RIGHTS_READ = &H20000
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_SET_VALUE = &H2
Public Const ERROR_FILE_NOT_FOUND = 2
Public Const SUCCESS = 0
Public Const REG_OPTION_NON_VOLATILE = 0
Public Const REG_SZ = 1
Public Const HKEY_LOCAL_MACHINE = &H80000002

Next Page