AlbReg, another Registration Tool ...

Albrecht Mengel, 13.9.96, mengel@stat-econ.uni-kiel.de

 REASON:  - I found four registry tools, but NO description. I hope this one
            gives a description and is good for your task.
 USAGE:   - Just copy the file into a directory in the delphi path,
            or set the path in the project options to it.
          - Then add AlbReg to the USES list in your program.
          - There are two procedures to search and alter registry values:
            read_registry_values and modify_registry_values.
	    For simpler use, there are two more procedures with simpler parameters
 PROBLEMS: - Not all types of Registry Variables are implemented.
             In this case occurs an error message. Please send me the
             call to the function with its parameters. I'll instantly try to
             implement it (if I find the same registry entry as you have
 Ideas (not yet implemented): - Allow wildcards in the variable
                              - Results Variables when key is proper and variable=''
 No Warranty !!!!!!!! Any use of AlbReg is on your own risk!!!!!!!!

Short description of Registry
-----------------------------

0. Excuse please:
   - my written english
   - I just begun with registry: No warranty of correct information!!!!!

1. What is Registry
Many programs have to store some informations (evaluation time, settings).
In former times the programmers used INI-files (xxxxxxxx.ini in C:\windows).
As the file name were restricted to eight characters, several products overwrote other
ini files with the same name.
Now it is modern to use the registry instead. It is a little data base with includes
most (all?) settings of windows and many application program settings.
The user is advised NOT to change things in registry as he may corrupt the operation system.
So it is useful for programmers to hide informations there, as most users are afraid to
look into the registry.

2. Repair Registry
If you change things there and destroy registry (to an unstable system), there is one hint:
Windows stores the database in two files: c:\windows\system.dat and c:\windows\user.dat.
When booting is successful, these two files are copied to system.da0 and user.da0.
So you can restore registry (in MSDOS mode) when you made a heavy fault.

3. Structure of Registry
Just look into registry with the program c:\windows\regedit.exe.
You can see an explorer-like window with a 'path' at the left side and variable with values
at the right side. There are six Roots (or trees) in the database:
HKEY_CLASSES_ROOT .. HKEY_DYN_DATA. They all have a directory/subdirectory-like structure.
The variables shown in the right part have names, types and values.
There are many types of a variables: Unknown, String, ExpandString, Integer, and Binary.
(I found only Strings and Binaries until now)
Every variable has an own type-symbol shown before its name.

4. Contents of the registry
Please look in the current PC magazines: They all have found registry an interesting item.
They give tips, how to tune windows95 and where to find what.
My informations here:
Hardware is found under HKEY_LOCAL_MACHINE \ Enum
Software is found under HKEY_CURRENT_USER \ Software

5. Use of registry
- Just look into it with regedit.exe or
- Use my little AlbReg demo. Just let the Variable and Conditions fields empty and
  try keys like:
      enum
      enum\*
      enum\*\*
      enum\*\*\*
  Then you can see the resulting keys (as paths)
- If you fill the variable field with
      Class
  you can see a list of may devices.
- Now we look after Drive Letters: fill the variable field with
      CurrentDriveLetterAssignment
- If you want to restrict the output to Disk Drives you can fill in a condition
  (in the condition field)
      Class=DiskDrive

6. Use of the AlbReg Unit
There are two procedures with do just what we did until now: read_registry_values and
modify_registry_values. The only ugly thing is the use of the StringLists.
They are needed, because the procedure wants many lines of input (the conditions)
and many lines of output (the result).
If you are shure that there is only one result and you need only one or no condition
then you may use the simplified procedures read_registry_value and modify_registry_value.
(The use of StringLists is hidden there)

7. Use of StringLists
They may be defined (locally or globally): var x:TStringList
Before use they MUST be created dynamically:  x:=TStringList.create
Then you can use them in the same manner as you use memo.lines or listbox.items
   x.add('Hello');   x.delete(7); for i:=0 to x.count-1 do memo1.lines.add(x[i]);
(the last thing can be done quicker:  memo1.lines:=x)
If you want to sort StringList, just set the property: x.sorted:=true
At last, when you are ready with all, free the memory:  x.free
(If you do not, the space is reserved and blocked until the next boot)

8. Special StringList:
I wrote mStrList, a unit with TmStrList, a descendent of TStringList,
capable of numeric/string/date sorting/searching up/down with key position/length.
This should solve all your sorting problems.
(Just get mStrGrid.zip from DSP and get mStrList.pas/mStrList.txt out of it)


9. The procedures of AlbReg
procedure read_registry_values(root:RootKeys; keys,variable:string; VAR conditions,result:TStringList);
 Reads Values in Registry. If variable='' then results all fitting keys
 Root: one of HKey_Classes_Root, HKey_Current_Config, HKey_Current_User, HKey_Dyn_Data, HKey_Local_Machine, or HKey_Users
 Keys: Full path before Variable name  (example: 'enum\scsi\*\*')
       You may use * and ? as Wildcards (enum\*or* -> Enum\Network and Enum\Monitor)
 Variable: Name of Entry.              (examples: 'CurrentDriveLetterAssignment'
                                                  'AutoInsertNotification')
 Conditions: StringList of numerical/string Comparisons
             'variable=number' or 'variable<>number' or
             'variable=string' or 'variable<>string' or
             'variable=' (Test on non-existence) or
             'variable<>' (Test on existence with any value)
                                       (example: 'class=DiskDrive')
 The two StringLists must be created and detroyed by the user.
 See the following example:
     letters:=TStringList.create;
     try conditions:=TStringList.create;
     try conditions.add('class=DiskDrive');
         read_registry_values(HKey_Local_Machine,'enum\esdi\*\*','CurrentDriveLetterAssignment',
                              conditions,letters);
     // The result is line after line in the StringList letters
     with letters do
      for i:=0 to count-1 do
       memo1.lines.add('Found Disk Drive '+strings[i]+':')
     finally conditions.free end;
     finally letters.free end;
     // Now you can see your IDE drive letters in the StringList letters
     // Remark: The registry uses to hold multiple entries of drives

procedure modify_registry_values(root:RootKeys; keys,variable,newvalue:string; conditions:TStringList);
 Looks like the above procedure in the registry and modifies all fitting variables to the value.
 The variable MUST exist.
 For security try first read_registry_values or the LookReg demo,
 that the correct variables are modified!

function read_registry_value(root:RootKeys; keys,variable:string; condition:string):string;
 Similar to read_registry_values, but simpler parameters:
 Only one condition is allowed. (If no condition, use '').
 The result is only the first found value.

procedure modify_registry_value(root:RootKeys; keys,variable,newvalue:string; condition:String);
 Similar to modify_registry_values, but simpler parameters:
 Only one condition is allowed. (If no condition, use '').

10. If you have any ideas or implementation problems, please send an email to me.

11. If you develop or correct anything in this AlbReg unit, let me know! I think, all
    users of AlbReg should participate.

11. Have fun (and stay tough)!

