Click here to Skip to main content
15,881,882 members
Articles / Mobile Apps

Using PocketPC Registry from C# Applications

Rate me:
Please Sign up or sign in to vote.
2.94/5 (8 votes)
7 Jun 2007CPOL2 min read 49.4K   481   19   5
The library for working with PocketPC registry
Pocket Registry Edit

Introduction

Registry is the system database for storing paires of Key and Value in the Windows operating systems. Windows CE system registry is organized as hierarchical (tree) structure. It is used to store configuration information for operating system, applications and hardware devices. Also, the registry contains information about installed applications, drivers and available hardware. Pocket PC registry is very similar to desktop Windows registry. You can use regedit utility to look at your desktop PC Structure.

Background

Pocket PC registry has four main root nodes & several additional:

  • HKEY_CLASSES_ROOT
  • HKEY_CURRENT_USER
  • HKEY_LOCAL_MACHINE
  • HKEY_USERS
  • HKEY_PERFORMANCE_DATA
  • HKEY_PERFORMANCE_TEXT
  • HKEY_PERFORMANCE_NLSTEXT
  • HKEY_CURRENT_CONFIG
  • HKEY_DYN_DATA

The functions for working with Pocket PC Registry, e.g. opening ROOT, creating keys and values, deleting nodes, enumerating childs are contained in coredll.dll system library. This set of functions are part of WinAPI.

Note from MSDN:
Windows APIs are dynamic-link libraries (DLLs) that are part of the Windows operating system. You use them to perform tasks when it is difficult to write equivalent procedures of your own.

To make use of PocketPC Registry API in the article we will consider DllImport attribute:

Note from MSDN:
The DllImport attribute provides a way to call functions in DLLs without type libraries. DllImport is roughly equivalent to using a Declare statement but provides more control over how functions are called.

Using the Code

The library contains a single class CERegister, which contains functions for setting and getting string values from registry:

  • RegGetStringValue
  • RegSetStringValue

The example of their usage:

C#
PocketRegistry.CERegister.RegSetStringValue(PitReg.RegRoot.HKEY_CURRENT_USER,
   "pocketpc\\CERegistry", "StringValue", "123");

Here also, some brief examples of DllImport Attribute usage:

Creating Registry Key

C#
//
// RegCreateKeyEx 
//

[System.Runtime.InteropServices.DllImport("coredll.dll")]
        private static extern int RegCreateKeyEx(
            uint hKey, 
            string lpSubKey, 
            uint Reserved, 
            string lpClass, 
            uint dwOptions, 
            uint samDesired, 
            uint lpSecurityAttributes, 
            ref uint phkResult, 
            ref uint lpdwDisposition 
            );

Open Registry Key

C#
//
// RegOpenKeyEx 
//
System.Runtime.InteropServices.DllImport("coredll.dll")]
        private static extern int RegOpenKeyEx( 
            uint hKey, 
            string lpSubKey, 
            uint ulOptions, 
            uint samDesired, 
            ref uint phkResult 
            );

Querying Registry Value

C#
//
// RegQueryValueEx
//

[System.Runtime.InteropServices.DllImport("coredll.dll")]
        private unsafe static extern int RegQueryValueEx( 
            uint hKey, 
            string lpValueName, 
            uint lpReserved, 
            out uint lpType, 
            byte * lpData, 
            ref uint lpcbData 
            );

There is a trick for getting string values. You should first retrieve the length of the value and then retrieve the value in the buffer:

C#
private static string GetStringValue(uint Key, string Name)
        {    
            uint buflen = 0;
            uint type = 0;
            char []str = null;
            unsafe
            {    
                                //Get the length of the value:
                int r = RegQueryValueEx(Key,Name,0,out type,null,ref buflen);
                if (type!=(uint)RegType.REG_SZ) throw new Exception (
                    "The key is not of string value");

                                //Create a buffer for the value:
                byte[] rez_buf = new byte [buflen];

                                //Retrieve the value to the buffer
                fixed(byte *charpointer=&rez_buf[0])
                {
                    int rez = RegQueryValueEx(Key,Name,0,out type,charpointer,ref buflen);
                    if (rez != 0) throw new Exception ("Can't get value");
                }

                str = new char [buflen];
                for (int i=0; i<buflen; i++)
                    str[i] = (char)rez_buf[i];

            }    
            return new string (str);
        }

Further Information

For further info you may refer MSDN

History

  • Submitted on 31 May 2007

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Netherlands Netherlands
Please visit my website for more articles

I'm software developer & Ph.D. student

Comments and Discussions

 
QuestionMajor item missing: Does not edit DWORD keys, only Strings or REG_SZ Pin
robertkjr3d23-May-16 7:19
robertkjr3d23-May-16 7:19 
GeneralMy vote of 1 Pin
LucianoGonzalez979-Apr-12 12:44
LucianoGonzalez979-Apr-12 12:44 
GeneralHey... Pin
Member 373145229-Jun-09 2:23
Member 373145229-Jun-09 2:23 
GeneralYou could just use Microsoft.Win32.Registry* Pin
Neil Cowburn1-Jun-07 13:31
Neil Cowburn1-Jun-07 13:31 
GeneralRe: You could just use Microsoft.Win32.Registry* Pin
Petro Protsyk3-Jun-07 5:51
Petro Protsyk3-Jun-07 5:51 
Well, thank you.
I was talking about .NET CF 1.0.

I'm also very glad about your Smart Device Framework.

Wish you good luck with it.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.