Wednesday, December 06, 2006

Windows Registry

So, I've been working with the registry for the last few weeks using various languages (VB6.0, C++6.0, C#, C++2005, and Python). The current issues I'm having with this are using the Mark Hammond win32api module for Python. Actually, the biggest problem I have with Python is the lack of documentation. They have an amazing documentation system, where comments in the code are automatically search-able and help files are created automatically. This is different than Visual Studio, where a help file has to be created automatically.

Anyway...back to the registry. I remember when I was first learning how to program using Windows, and I started using .conf files. All of my programs had a CONF file which had no keys or sections, but just values. I had to write classes to read and write specific lines at a time, and ended up just parsing the whole thing, splitting on newlines, and then getting the offset. The problems with this are obvious. So, I read up on it, and realized there were some great INI parsing utilities with VB6.0. Once I used those in C++6.0, I read in depth on it, and realized that the reason the functions, i.e. GetPrivateProfilexxx, are the same, is because there used to be a windows.ini file, which all applications wrote to. Well...how's that for inane! I guess the idea is similar to the old school pre-DNS look-up tables.

So far, however, I've found that getting to the point of understanding what is necessary to even read values from the registry is to use AfxGetApp() (in C++ dialog based apps) in your MyApp class. Then, you can read and write as much as you want in InitInstance and ExitInstance. In Python, it's easier, but at least I understand what they want. In VB it's a joke. Just call "SaveSetting" or "GetSetting". Gah, I don't even know why I had a problem in the first place or what the point of this is other than to point out that although the API calls are all different (in different languages), accessing the registry is as easy as declaring that it exists, and providing the "path" or folder list to which keys you want to read.

It's good practice, however, to read from the HKEY_CURRENT_USER\Software folder, especially for settings related to the specific user. It's a better idea to use HKEY_LOCAL_MACHINE\Software if you want to store settings like database names, usernames and passwords, and things that are general settings and probably used for remote connections (also for IRC server lists, etc). Don't change HKEY_USERS, because this is the default setup for new users to the system, and HKEY_CLASSES_ROOT is just bizarre, and I don't want to get into it.

Back to Python!