An S-language interface to the Windows Registry


This package provides a basic interface to the Windows registry. There are S functions and C routines for basic investigation of the contents of the registry, allowing one to find out the names of the sub-keys and the names of the keys that contain actual values. It also allows one, of course, to retrieve the value of a particular key and to set the value of a key, including create a new one.

There are numerous facilities that we could add from the Windows API, but have not done so -- yet! Please ask if you need more functionality added. Ideally, please send some code that implements it also and I will add it to the package so that there is a single package which has all the functionality.

Get the names of the Sub-Keys

If one uses the Regedit tool, you can see that there are several primary keys under the root key My Computer. These are the builtin keys and are also available in R in the variable .BuiltinKeys. Notice that each of these has a + beside it, indicating that there are sub-keys which you can explore if you click on that key. For instance, if you select HKEY_LOCAL_MACHINE you'll see 5 other keys under that named HARDWARE, SAM, SECURITY, SOFTWARE and SYSTEM. (This is on my Windows 2000 machine. Your's may differ slightly.) All of these except SECURITY has sub-keys. If we select SOFTWARE, we get a reasonably length list of different pieces of software installed on this machine. Some of these have sub-keys, and some do not. Since I have Perl installed, it appears in this list and it has no sub-keys. It does however have a value within this key, specifically BinDir. This tells us where on the system the Perl executable is located; a useful thing to know. We can get the values of the keys within a "key folder" using the S function getRegistryKeyValues.
 getRegistryKeyValues("SOFTWARE\\Perl", top = "HKEY_LOCAL_MACHINE")
Note that we give the top-level key via the argument top and then specify the path to the collection of values as "SOFTWARE" and then "Perl". We separate the elements in this path using \ which we must specify as \\ in R.

Rather than explicitly specifying the name of the top-level key, we can specify the index in the .BuiltinKeys vector.
 getRegistryKeyValues("SOFTWARE\\Perl", top = 4)

Alternatively, we can use a short-hand version by allowing partial matching to find the relevant index.
 getRegistryKeyValues("SOFTWARE\\Perl", top = "HKEY_LOC")

In the near future, we will also allow a single path
 getRegistryKeyValues("HKEY_LOCAL_MACHINE\\SOFTWARE\\Perl")

What this returns us is a named list. The names are the keys. The elements are the values in the registry converted as appropriate. Strings are converted to S strings, DWORD values are converted to integers (currently).

Note

We'll add more facilities for this soon.
But what if we didn't have Regedit? How would find out the names of the sub-keys within a key? We can always call getRegistryKeyValues but this only identifies keys that actually have values. It omits sub-keys that have sub-keys, i.e. key folders. To find these, we can use the function getRegistrySubKeyNames.
  getRegistrySubKeyNames("SOFTWARE", 4)
> getRegistrySubKeyNames(c("Control Panel", "Desktop"), top = 3)
[1] "WindowMetrics"
Note from this example that we can also specify the elements or terms in a path as separate elements of a character vector as opposed to separating them with \\\\.

Setting a value

The function setRegistryValue allows one to set a value within the registry. One specifies the path to the key folder and the name of the key within that folder to identify the specific key to be set. Then one sets

Identifying nodes and paths

This package offers a variety of different ways to specify a path in the registry to identify a node or key.
 .resolveToplevelRegistryKey(.BuiltinKeys[1], "Control Panel")
 .resolveToplevelRegistryKey(.BuiltinKeys[1], "Control Panel\\Panel")
 .resolveToplevelRegistryKey(.BuiltinKeys[1], "HKEY_CLASSES_ROOT\\Control Panel\\Panel")
 .resolveToplevelRegistryKey(.BuiltinKeys[1], c("HKEY_CLASSES_ROOT", "Control Panel", "Panel"))