Working with registry on Pocket PC
Vassili Philippov (vasja@spbteam.com), January 21, 2002.
Summary
Working with registry on Pocket PC is very similar to working with it in
desktop Windows. Nevertheless, there are some differences. Registry is not
stored in a file on Pocket PC. There are some undocumented possibilities to change
settings of built-in applications that are Pocket PC specific. Third-party
libraries and controls will also simplify working with registry in your eVC++ and
eVB programs.
eMbedded Visual C++
Let us consider one of the most frequently used registry operations - reading
string values - using different libraries. We will read 2 values - "name" and "surname"
from HKEY_CURRENT_USER\Software\pocketpcdn\RegistrySample.
Win API
Using Win API functions to work with registry, you should check that you
close registry keys in all paths. To read a string value you should call RegQueryValueEx
twice: once to get size and only then to read the value.
HKEY hKey = NULL;
DWORD dwDisposition = 0;
if (RegCreateKeyEx(
HKEY_CURRENT_USER,
_T("Software\\pocketpcdn\\RegistrySample"),
0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition
) != ERROR_SUCCESS)
{
AfxMessageBox(_T("Cannot read values"));
return;
}
DWORD dwType = REG_SZ;
DWORD dwDataSize = 0;
CString strName;
if (RegQueryValueEx(
hKey, _T("name"), 0, &dwType,
(PBYTE)NULL, &dwDataSize
) == ERROR_SUCCESS)
{
RegQueryValueEx(
hKey, _T("name"), 0, &dwType,
(PBYTE)(LPTSTR)strName.GetBuffer(dwDataSize + 1),
&dwDataSize
);
strName.ReleaseBuffer();
}
CString strSurname;
if (RegQueryValueEx(
hKey, _T("surname"), 0, &dwType,
(PBYTE)NULL, &dwDataSize
) == ERROR_SUCCESS)
{
RegQueryValueEx(
hKey, _T("surname"), 0, &dwType,
(PBYTE)(LPTSTR)strSurname.GetBuffer(dwDataSize + 1),
&dwDataSize
);
strSurname.ReleaseBuffer();
};
RegCloseKey(hKey);
AfxMessageBox(strName);
AfxMessageBox(strSurname);
ATL
ATL wraps Win API and takes care about closing registry keys (it is done automatically
in destructor). Similarly to Win API you should read value in two calls.
CRegKey key;
key.Create(HKEY_CURRENT_USER, _T("Software\\pocketpcdn\\RegistrySample"));
DWORD dwSize = 0;
CString strName;
if (key.QueryValue(NULL, _T("name"), &dwSize)==ERROR_SUCCESS) {
key.QueryValue(strName.GetBuffer(dwSize+1), _T("name"), &dwSize);
strName.ReleaseBuffer();
}
CString strSurname;
if (key.QueryValue(NULL, _T("surname"), &dwSize)==ERROR_SUCCESS) {
key.QueryValue(strSurname.GetBuffer(dwSize+1), _T("surname"), &dwSize);
strSurname.ReleaseBuffer();
}
AfxMessageBox(strName);
AfxMessageBox(strSurname);
VORegistry
The latest version of VORegistry library
supports all features and is very easy in use. First of all I want to give you a small
example of the same operation (reading two strings from registry) using different
methods (Win API, ATL, VORegistry):
CVORegistry reg(HKEY_CURRENT_USER, _T("Software\\pocketpcdn\\RegistrySample"));
CString strName = reg.ReadString(_T("name"));
CString strSurname = reg.ReadString(_T("surname"));
AfxMessageBox(strName);
AfxMessageBox(strSurname);
As you see VORegistry provides the simplest
way of using registry in eVC++. It contains methods for working with all types
of values (string, DWORD and binary), creating/deleting and enumerating subkeys,
deleting values and keys.
eMbedded Visual Basic
It is more difficult to read/write registry if you use eMbedded Visual Basic.
You can use Win API functions. Use API Text Viewer tool from eMbedded Visual Tools
to get declare of necessary Win API function. Here are declares of some registry related
functions:
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
Or you can use third-party controls like:
that simplify using registry.
Read more about using registry in eMbedded Visual Basic in deVBuzz articles:
Edit Pocket PC Windows CE Registry using eVB (eMbedded Visual Basic) and Windows CE API calls
eVB sample accessing the Pocket PC Windows CE Registry
Interesting places in registry
Most programs keep settings in registry and you can customize many applications by changing
the corresponding value. There are a lot of undocumented features that you can use. For example you can change:
- TCP/IP configuration
- Files associations
- System fonts
- Scroll bars sizes
- etc
You will find a lot of information about such tricks at
http://www.phm.lu/PocketPC/RegTweaks.
Remote Registry Editor
eMbedded Visual Tools include Remote Registry Editor that is useful
for exploring registry of connected Windows CE devices or of the emulator. It
works very similar to desktop Registry Editor.
If you want to check what is changed in registry you can export registry before the operation
and after. Then use WinDiff to find difference. Exporting the whole registry
can take about an hour if you use slow COM connection.
Pocket PC registry editors
There is no Microsoft Pocket PC registry editor that works on Pocket PC but
you can use one of many third-party registry editors. Here are some of them:
Related resources:
-
http://www.pocketpcdn.com/sections/fileregistry.html
Section: Files and Registry
-
http://www.pocketpcdn.com/articles/recreg.html
QA: How to copy registry key recursively?
-
http://www.pocketpcdn.com/libraries/voregistry.html
Library: VORegistry
-
http://www.pocketpcdn.com/libraries/vbceutil.html
Control: vbceUtil v2
-
http://www.pocketpcdn.com/libraries/idssapi.html
Control: IDSSAPI
- http://www.microsoft.com/mobile/developer/technicalarticles/registry.asp
Article: Using the Registry in a C++ Application
- http://www.cegadgets.com/winceregfaq.htm
Article: Windows CE Registry FAQ
- http://www.devbuzz.com/content/zinc_eVB_and_CE_registry_II_pg1.asp
Article: eVB sample accessing the Pocket PC Windows CE Registry
- http://www.devbuzz.com/content/zinc_eVB_and_CE_registry_pg1.asp
Article: Edit Pocket PC Windows CE Registry using eVB (eMbedded Visual Basic) and Windows CE API calls