by Christian Feichtner
0. DISCLAIMER :
This article reflects my personal experiences with the registry and Delphi. I had no 'real' documentation on this, except what shipped with Delphi. I will not take any responsibility that occurs from the usage of the procedures described in this article. The same applies to the usage of the accompanying REGDLL.DLL and its interface. USE AT YOUR OWN RISK.
y-database as an 'INI file'. Especially with the advent of Windows 95 every 'good' windows application should use the registry database to store its information.
Note that the described API routines are from the 16bit API. They work well with the registry of Windows 95, but are not capable of using the special new features of Windows 95.
1. What is the registry ?
The registry is a heirarchical database, which is used to store information for the whole system. OLE-apps made frequent use of the registry in Win31. In Windows 95 the registry has grown to more than that. It not only stores system information but has become a total replacement for the old-style INI files. The INI files are only supported to maintain compatibility for 'old' 16bit Apps.
2. What does the registry look like ?
As mentioned above, the registry is a heirarchical database. It is organized as a tree. The most interesting key (and the only one accessable from Delphi with the 16bit version) is the HKEY_CLASSES_ROOT.
This key can be used to store application settings. (Thus, I think there is another key for Windows 95 apps. Since Delphi can only access this key, you can use it until Delphi-32 becomes avaliable).
Example:
+ HKEY_CLASSES_ROOT This is what a key could look like. Assume an
| application named Information Manager (which I'm
+--+-.IFM currently developing) which saves its files with the
| extension .IFM. Under Win95 the shell, open, command
+--+-shell and ShellNew keys are of special interest. (Yes they
| can be used with Delphi as well.)
+--+-open
| |
| +---command
|
+-ShellNew
.IFM\shell\open\command defines the command to be executed when the user double clicks on the file (or under Win95 hits the right mouse button and selects open).
The keys alone won't do the job. Normally there are values assigned to the keys. Under Win31 these can only be strings. Win95 defines a kind of binary and a DWORD as well.
The shell\open\command normally has a value like:
Name Value
+ HKEY_CLASSES_ROOT
|
+--+-.IFM
|
+--+-shell
|
+--+-open
| |
| +---command (default) "H:\PROJECT\INFOMAN\IFM.EXE %1"
|
+-ShellNew
The selected filename will be substituted for '%1' and passed along as a command-line parameter to the application. Your Delphi app can use PARAMSTR(x) to get the x-th command line parameter. x=0 returns the full path and name of the application itself.
If you are using the preview of Win95 and want your application to have an entry in the 'New' popup menu (something like 'Information Manager 1.0 file'), you have to do the following:
Add a new (text) value for the ShellNew key, named NullFile, with a value of "". Also name the extension (.IFM) equal to the entry of your app in the registry. If the application has an entry named 'InfoMan', then name .IFM as InfoMan.
Example:
Name Value
+ HKEY_CLASSES_ROOT
|
+--+-.IFM (default) "InfoMan"
|
+--+-shell
|
+--+-open
| |
| +---command (default) "H:\PROJECT\INFOMAN\IFM.EXE %1"
|
+-ShellNew NullFile ""
Now for the key for the application itself. (I assume the application is still Information Manager (short: InfoMan)).
The whole tree looks like this:
Name Value
+ HKEY_CLASSES_ROOT
|
+--+-InfoMan (default) "Information Manager 1.0 File"
|
+--+-Misc
|
+--+-Options
| |
| +---Saving
| |
| +---Directories
|
+--+-shell
|
+--+-open
|
+---command (default) "H:\PROJECT\INFOMAN\IFM.EXE %1"
The Options key contains several other subkeys, which store the application- specific settings like the window position, delete confirmations, and others.
3. How to read and write data to the registry
Delphi offers the following API-routines for accessing the registry:
- RegCreateKey()
- RegOpenKey()
- RegDeleteKey()
- RegCloseKey()
- RegEnumKey()
- RegQueryValue()
- RegSetValue()
NOTE: These functions are from the Win31 API. These functions can only read and write string (PChar) values and can not set the name of a key.
Before a key can be accessed, it must be opened. The open functions return a handle (HKEY), which is used to access the subkeys.
3.1 RegCreateKey()
Opens a key and if the key does not exist, it will be created.
function RegCreateKey(Key: HKey; SubKey: PChar; var Result: HKey): Longint;
- Key:
- The handle of the key which should be accessed. To write directly under the root, you can use HKEY_CLASSES_ROOT.
- SubKey:
- The subkey to be accessed.
- Result:
- The resulting key-handle.
- Returns:
- ERROR_SUCCESS, if the function was successful, otherwise it will be an error value.
3.2 RegOpenKey()
Opens an existing key. Unlike RegCreateKey, a non existing key returns an error and wi