by Aaron Ballman

Before we begin talking about code, let's get a little background information about what the registry is and how it works on Windows. The registry on Windows is a repository of key-value pairs in a file system-like structure. If you can understand how the Windows Explorer works, then you will have very little problems navigating your way around the registry.

To view the registry, go to Start->Run and type in "regedit" to bring up the system's registry editing tool. You can use this to navigate the registry while I explain in a bit more detail about what you're looking at.

The first thing you will notice once you collapse all the folders is that in the base of the registry there are five folders. These are special folders called "hives", and there will always be exactly 5 of them there (well, Microsoft may choose to make another one some day, but you can be sure that these 5 will always exist on every user's machine). Each hive is used for something different. For example, HKEY_CLASSES_ROOT is used to define things pertaining to the file system like file extensions and what applications they correspond to, what icon goes with what file extension, etc. HKEY_CURRENT_USER has all the registry keys for the currently logged-in user (such as preferences for the look and feel of Windows or other applications). HKEY_LOCAL_MACHINE is used for system-wide functionality (such as what human-readable drive names go with what drive). The last two (HKEY_USERS and HKEY_CURRENT_CONFIG) are rarely used, so I won't describe them here.

All registry keys (and remember, keys are not folders, and folders are not keys) have a basic type to them. The standard basic types are: string, binary, DWORD, multi-string and expandable string. REALbasic only allows you to store values as a string or DWORD currently, and handles the type information for you automatically. Also note that as of version 5.0, RB does not read in anything other than a DWORD or a string value.

When using REALbasic to deal with the registry, there are only a few concepts you need to be aware of. Traversal of the tree structure is much the same as how you traverse the FolderItem's structure. You can use .Parent to get any folder's parent folder, and .Child to get and folder's child folder. However, you cannot get the parent of a hive (one of the HKEY_* folders) since they have no parent. The other concept you need to know about is how to access registry keys. There are two different types of keys (but they're conceptually very similar). Every folder can have what is called a "default key". This is basically a key that is associate with the folder and has no name. Inside of a folder there may be many keys, but the folder itself can only have one default key. You just need to know that the default key has to be accessed in a special way from within REALbasic that is different from the way you would access a normal key.

So to sum it all up, you traverse through different folders in the registry with the Child and Parent methods, and you read or write to the various keys using Value and DefaultValue methods. Let's delve a little deeper into how exactly you would do this stuff in RB.

In order to start doing anything useful with the RegistryItem class, you need to first create a RegistryItem object that points to the registry folder you want to operate on. For our example, let's get the registry folder that corresponds to the system information about where special folders are located. To do this, you just need to use the new operator and pass in the path to the folder in question:

myItem = new RegistryItem("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion")
This will give you an object that points to the specified folder. Note that if you do not have sufficient access privileges for the folder, you will get a RegistryAccessErrorException. This applies to all calls for accessing or modifying registry items. Also note that the path will be created if it does not currently exist (so make sure to check your spelling!).

So now that we have a valid RegistryItem, what can we do with it? Let's just print out all the information from this folder so we can see what all is in there. We do this using the KeyCount, Value and Name methods.

for i = 0 to myItem.KeyCount - 1
	ListBox1.AddRow( myItem.Name( i ) + " = " + myItem.Value( i ) )
next

So now that we've seen how to query different values from a registry folder, let's take it one step further and see how to properly add our own application's registry information. For this example, we are going to assume your company name is FooBar, Inc and the product you are making is called "Spiffy FooBarinator".

First, we should get a handle to the current user's registry information for installed applications. Once we have that, we need to create the folder for our company name. If that folder already exists, it will simply open the folder up for us. Once we are in the company folder, we should try to create the product's folder.

// Get the base item in the software keys for our company and product
myBaseItem = new RegistryItem("HKEY_CURRENT_USER\Software\FooBar, Inc\Spiffy FooBarinator")

Now we will have created (or opened) the folder up for the product we are interested in. We can begin adding the items that interest us. For this example, we will store the user's name, their companies name and their product key in the registry. Also, we will make a subfolder that will contain a few different keys with non-string data types. Let's start out by saving the user's name, company name and product key.

// Create the string-based items for the user's information
myBaseItem.Value("Name") = "Gordon Gano"
myBaseItem.Value("Company") = "The Violent Femmes"
myBaseItem.Value("Product Key") = "1234567-I forget what 8 was for-910"

Now let's make the sub-folder and put some values into it. See just how easy all of this is!

// Create the new folder called "Other Values"
myBaseItem = new RegistryItem("HKEY_CURRENT_USER\Software\FooBar, Inc\Spiffy FooBarinator\Other Values")

// Start putting some value into it
myBaseItem.Value("What Number Again?") = 8
myBaseItem.Value("Answer to meaning of life") = 42
myBaseItem.Value("Isn't the registry easy to use now?") = "Yes!"

There are a few last things we should talk about before I wrap this up. One of those things is safety. Remember that the registry on Windows is what drives a large amount of the operating system and other people's applications. It's very easy to break something in the registry, so always use caution when you start changing values or deleting things. Once you've removed something from the registry, it is gone forever (well, unless you made a backup of the folder branch you deleted). So use caution when removing or changing items around that you do not create yourself! The other thing to think about it etiquette related. Although you have access to every application's registry keys, you will get a lot of bad karma if you go mucking with them without first asking the end user. A good example of this is changing file extension handlers. You can associate your application with a file extension via the registry, but you should _always_ ask the end user whether they want this to occur. Failing to do so only tends to infuriate the end user (trust me on this one, I won't use any application that steals file extensions without asking me first).

That's all there is to it! If you have any questions or comments, feel free to send them to aaron@realsoftware.com I hope you found this little tutorial helpful! To download the example that goes along with this, click here.