Working with the Windows Registry in C++ (Page 1 of 4 )
The Windows Registry is a crucial part of each Microsoft Windows-based operating system. It acts exactly like a directory, storing all kinds of user settings, options, and configuration data about applications, file associations, user policies, and so forth. Therefore, the ability to work with the registry is something that can’t be missing from the arsenal of a software programmer. In this article we are going to learn how to accomplish this task in C++.
We are going to approach this situation using dedicated API functions that allow us to work with the registry. During this article we’ll learn how to create a console-based application that will be able to do the following: open a specified subkey, create and set a new value, create a new subkey, delete subkeys and values, and dump the content of a particular “tree” (or the whole registry) into a file. We won’t use additional libraries!
However, at first we’re going to refresh our memory of the structure of the registry so that we crush any possible dilemmas regarding particular terminology such as keys, values, subkeys, name-data pairs, trees, hive, and such. Then we will proceed to creating our application, taking a step-by-step approach while thoroughly explaining the correct usage of the registry functions.
I wouldn’t classify the required skill level for this tutorial higher than intermediate. Learning how to work with the registry using the soon-to-be-presented functions is almost as easy as learning a Hello World example, if you already know the way the registry works. I foresee that following along will be easy even for a beginner that is familiar with the syntax of C++.
All of this being said – I invite you to join us for the tutorial.
The Basics
It’s important to understand the structure of the registry so that there won’t be any confusion during our explanations later on. Here we’ll be very quick and right on topic because we don’t have time to waste. First of all, you may imagine the registry as a folder. It can have folders and files within it. Now let’s see how we call these.
The Windows registry contains the following two elements: keys and values. Keys stand for folders of a sort (branches) while the values represent files in our analogy. In reality, values are name-data pairs, meaning that a value is split into two parts: name and data. The name is used for identification purposes and the data part is the actual data.
Each of the keys can have values but also subkeys. The entire registry is split into logical sections that are called hives. These are the HKCR, HKCU, HKLM, HKU, and HKCC. The whole registry in its entirety is sometimes called the root hive.
Now my final suggestion before we begin is that you should open your Registry Editor (regedit.exe or regedt32.exe) and look around a bit. Once you can find your way around with ease inside the structure of the your registry, you should be prepared. Oh, and you should backup your registry just in case. However, we won’t do any harm!
Next: Basic Registry Functions >>
More Windows Scripting Articles
More By Barzan "Tony" Antal