|
| |
Registry Example
|
With
VBCE 6.0, you can now create more professional applications by storing program specific
data in the CE Registry! Here is a simple example which shows how to read/write to
the CE Registry.

|
Attribute VB_Name = "modRegistryAPI"
Public Const NO_ERROR = 0 ' Function returned successfully.
Public Const ERROR_NONE = 0
Public Const HKEY_CURRENT_USER = &H80000001 ' Reference a section of the registry.
Public Const HKEY_LOCAL_MACHINE = &H80000002
' This is a simplified version of the declaration in WINCEAPI.TXT
Public Const KEY_ALL_ACCESS = &H3F
Public Const REG_DWORD = 4 ' 32-bit number.
Public Const REG_OPTION_NON_VOLATILE = 0 ' Key is preserved when system is rebooted.
Public Const REG_SZ = 1 ' Unicode nul terminated string.
Declare Function RegCloseKey Lib "Coredll" ( _
ByVal hkey As Long) _
As Long
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, _
ByVal lpSecurityAttributes As Long, _
phkResult As Long, _
lpdwDisposition As Long) _
As Long
Declare Function RegDeleteKey Lib "Coredll" Alias "RegDeleteKeyW" ( _
ByVal hkey As Long, _
ByVal lpSubKey As String) _
As Long
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
Declare Function RegQueryValueExLong Lib "Coredll" Alias "RegQueryValueExW" ( _
ByVal hkey As Long, _
ByVal lpValueName As String, _
ByVal lpReserved As Long, _
lpType As Long, _
lpData As Long, _
lpcbData As Long) As Long
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 Long, _
lpcbData As Long) _
As Long
Declare Function RegQueryValueExString 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
Declare Function RegSetValueExLong Lib "Coredll" Alias "RegSetValueExW" ( _
ByVal hkey As Long, _
ByVal lpValueName As String, _
ByVal Reserved As Long, _
ByVal dwType As Long, _
lpValue As Long, _
ByVal cbData As Long) _
As Long
Declare Function RegSetValueExString Lib "Coredll" Alias "RegSetValueExW" ( _
ByVal hkey As Long, _
ByVal lpValueName As String, _
ByVal Reserved As Long, _
ByVal dwType As Long, _
ByVal lpValue As String, _
ByVal cbData As Long) _
As Long
Public Function CreateNewKey(lSection As Long, _
sNewKeyName As String)
Dim hNewKey As Long '-- Handle to the new key
Dim lRetVal As Long '-- Result of the RegCreateKeyEx function
'-- Create Registry Key
'-- If key already exists, nothing happens
lRetVal = RegCreateKeyEx(lSection, sNewKeyName, CLng(0), _
vbNullString, REG_OPTION_NON_VOLATILE, _
KEY_ALL_ACCESS, _
CLng(0), hNewKey, lRetVal)
'-- Return Handle to Key
CreateNewKey = hNewKey
'-- Close Registry Handle
RegCloseKey (hNewKey)
End Function
Public Function QueryValue(lSection As Long, _
sKeyName As String, _
sValueName As String)
Dim lRetVal As Long 'result of the API functions
Dim hkey As Long 'handle of opened key
Dim vValue As Variant 'setting of queried value
'-- Open Registry Key
lRetVal = RegOpenKeyEx(lSection, sKeyName, 0, _
KEY_ALL_ACCESS, hkey)
'-- Query Registry Value
lRetVal = QueryValueEx(hkey, sValueName, vValue)
'-- Return Value
QueryValue = vValue
'-- Close Reg Key
RegCloseKey (hkey)
End Function
'SetValueEx and QueryValueEx Wrapper Functions:
Public Function SetValueEx(ByVal hkey As Long, _
sValueName As String, _
lType As Long, vValue As Variant) As Long
Dim lValue As Long
Dim sValue As String
Select Case lType
Case REG_SZ
sValue = vValue & Chr(0)
SetValueEx = RegSetValueExString(hkey, sValueName, CLng(0), _
lType, CStr(sValue), Len(sValue) * 2)
Case REG_DWORD
lValue = vValue
SetValueEx = RegSetValueExLong(hkey, sValueName, CLng(0), _
lType, CLng(lValue), 4)
End Select
End Function
Function QueryValueEx(ByVal lhKey As Long, _
ByVal szValueName As String, _
vValue As Variant) As Long
Dim cch As Long
Dim lrc As Long
Dim lType As Long
Dim lValue As Long
Dim sValue As String
'-- Determine the size and type of data to be read
lrc = RegQueryValueEx(lhKey, szValueName, CLng(0), lType, CLng(0), cch)
Select Case lType
'-- For strings
Case REG_SZ:
sValue = String(cch, 0)
lrc = RegQueryValueExString(lhKey, szValueName, CLng(0), lType, _
sValue, cch)
If lrc = ERROR_NONE Then
vValue = Left(sValue, cch - 1)
Else
vValue = Empty
End If
'-- For DWORDS
Case REG_DWORD:
lrc = RegQueryValueExLong(lhKey, szValueName, CLng(0), lType, _
lValue, cch)
If lrc = ERROR_NONE Then vValue = lValue
Case Else
'-- All other data types not supported
lrc = -1
End Select
End Function
Public Sub SetKeyValue(lSection As Long, _
sKeyName As String, _
sValueName As String, _
vValueSetting As Variant, _
lValueType As Long)
Dim lRetVal As Long '-- Result of the SetValueEx function
Dim hkey As Long '-- Handle of open key
'-- Open the specified key
lRetVal = RegOpenKeyEx(lSection, sKeyName, _
0, _
KEY_ALL_ACCESS, hkey)
'-- Set New Value
lRetVal = SetValueEx(hkey, sValueName, lValueType, vValueSetting)
'-- Close Reg Key
RegCloseKey (hkey)
End Sub
|
|