5,702,921 members and growing! (23,524 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Mobile Development » General     Intermediate

Using PocketPC Registry from C# Applications

By Extrim

The library for working with PocketPC registry
C++, C# 2.0, C#, Windows, .NET CF, .NET, Mobile, Win Mobile, CE .NET 4.1VS2005, Visual Studio, Dev

Posted: 30 May 2007
Updated: 7 Jun 2007
Views: 14,001
Bookmarked: 9 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 1.99 Rating: 2.85 out of 5
2 votes, 40.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
1 vote, 20.0%
4
2 votes, 40.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article
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:

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

Here also, some brief examples of DllImport Attribute usage:

Creating Registry Key:

//

// 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:

//

// 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:

//

// 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:

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 ;

                                //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 ;
                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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Extrim


Please visit my website for more articles

I'm software developer & Ph.D. student

Occupation: Software Developer (Senior)
Location: Ukraine Ukraine

Other popular Mobile Development articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralYou could just use Microsoft.Win32.Registry*memberNeil Cowburn14:31 1 Jun '07  
GeneralRe: You could just use Microsoft.Win32.Registry*memberExtrim6:51 3 Jun '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 7 Jun 2007
Editor:
Copyright 2007 by Extrim
Everything else Copyright © CodeProject, 1999-2008
Web20 | Advertise on the Code Project