Using the Windows CE Registry
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.
case IDM_ADD_KEY: //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.
Figure 3: Windows CE Reserved Handles
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 ); break;
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