I learned of two rather interesting RegistryItem factoids while playing around tonight that I thought I'd share with others. One appears to be an oversight with the API and a way around it, while the other appears to be a mysterious bug that took a while to track down (and a way around it as well).
First, the oversight. It appears that there is no way to get the name of a *folder* for a given registry item -- you can only get the names of *keys*. This can be rather problematic if you're attempting to search both names and values of everything in the registry. The RegistryItem.Name API only gives you access to key names (for key/value pairs). To get around this, you can make use of the RegistryItem.Item method to get the folder by index, and the Path property to figure out the folder's name. The code would look something like this:
Function FolderName( extends r as RegistryItem, index as Integer ) as String
dim child as RegistryItem = r.Item( index )
dim parts() as String = Split( child.Path, "\" )
return parts( UBound( parts ) )
End Function
This code obviously needs some error checking, commenting and what-not, but you get the idea. Find the child item by index, get its path, and return the last part of the path -- that'll give you the folder's name. A bit inefficient, but certainly functional in a pinch.
The other problem I ran into was with regards to deleting folders from a registry item. It turns out that RegistryItem.Delete can be used on both keys as well as folders, but the behavior with folders can be a bit strange. If you're on an NT-based system (Win2k and up are the supported platforms for current versions of REALbasic), then you need to pass in an ASCII string for the folder name, otherwise it will not be deleted. This has two interesting ramifications. The first, and hopefully most obvious one, is the lack of Unicode support. It means you cannot delete any folders which have a Unicode character in their path. The second ramification stems from the fact that UTF-16 strings containing only ASCII data are not ASCII strings themselves (like UTF-8 strings are) -- so if you get the data from an external source (like a Win32 API using WStrings), then the call will still fail (silently, no less). The way around this is to always convert to ASCII before passing the Name off to RegistryItem.Delete. However, this will still fail to work if you have any non-ASCII characters in the path -- a very annoying limitation, to be sure!
Hopefully this will be of interest to anyone working with the Registry on Windows in REALbasic.
How do some of those registry programs that do so called "house cleaning" grab the folder names? Same way or something different?
There are Win32 APIs to handle this sort of thing -- the problem is that REALbasic simply doesn't expose an API to you to handle it.
I should note that I filed a feature request to be able to access folder names:
http://www.realsoftware.com/feedback/viewreport.php?reportid=bcxsorso
cool. Might come in handy some day. I signed on.
Thanks.
Erm.. You filed a feature request? I wouldn't have ever thought you'd have to jump through the same hoops we do!
@Christopher -- yup, and I file bug reports as well. These things have to get scheduled time somehow. :-)