February 27, 2021
Hot Topics:

Using the Windows CE Registry

  • By Nancy Nicolaisen
  • Send Email »
  • More Articles »

Adding Registry Keys

You can add Registry keys with either the RegOpenKeyEx() or RegCreateKeyEx() function. The advantage of using RegCreateKey() is that it creates a key if the key doesn't already exist as well as opening it. In the RegDemo example, we use RegCreateKeyEx() in response to the IDM_ADD_KEY message.

//if !n2
    //reg me
//else open my key
rc = RegCreateKeyEx (HKEY_LOCAL_MACHINE,
                     TEXT("Software\\n2"), 0,
                     TEXT (""), 0, 0, NULL,
                     &hKeyN2, &dwDisp);

The parameters to RegCreateKey, in the order shown, are the handle to the parent key; the name, as aUnicode string, of the key to add; a NULL placeholder for a reserved parameter; the class name of the key as aUnicode string; three more place holders set to 0,0, and NULL respectively; the address of a variable to receive the returned handle if the key is created successfully; and the address of a Variable to receive the disposition of the call.

Notice that the value of the first parameter is HKEY_LOCAL_MACHINE, which corresponds to one of the root key names in the Registry. Used in this context, HKEY_LOCAL_MACHINE identifies a reserved handle to this key, and can be treated just as though it were returned by RegCreateKey() or RegOpenKey(). There are four reserved key handles you may use to access the top level of the CE Registry.

Figure 3: Windows CE Reserved Handles

Handle Name

Now, take a look at the second parameter:


Notice that we have descended two levels in the Registry tree to create and open the key "n2". You don't have to traverse the Registry's hierarchy one step at a time if you know the full name of the key you want to open.

Finally, make note of the dwDisp parameter. After a successful return, dwDisp will contain one of two values: REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY. Use this value to detect whether or not the key existed before the call.

Skipping over the message box code, we finish the handling of this case by closing the key handle. This is a very important step because leaving the key open may cause subsequent accesses to fail.

//clean up after ourselves
RegCloseKey( hKeyN2 );

Looking Ahead

In the next installment, we'll continue exploring the RegDemo example, and learn how to access and enumerate Registry keys.

About the Author

Nancy Nicolaisen is a software engineer who has designed and implemented highly modular Windows CE products that include features such as full remote diagnostics, CE-side data compression, dynamically constructed user interface, automatic screen size detection, entry time data validation.

In addition to writing for Developer.com, she has written several books including Making Win 32 Applications Mobile.

# # #

Page 2 of 2

This article was originally published on October 29, 2003

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date