Unit RegWork

Copyright (c) 1996 Stryder Software Development All rights reserved by Dale Clarke This unit is freeware and can be used in any way you wish. I use this code in all projects that require accessing the widows registry. You use this code at your own risk. I will not be responsible for any damages or los of revenue from the use of this unit. To Use this Unit: If recieved from my web site then remove or comment the first line and save to your Delphi/Lib directory. Add RegWork to the Uses Statment in the calling unit. This unit wraps tRegistry and Win 32 API's to create a easy registry managment unit. You must already have the Registry.pas unit in your Delphi/Lib directory to use this unit. Registry.pas can be obtained on the internet by doing a search for TRegistry as the key word.

Classes

Functions

ChangeRegistryBool - Writen by Dale (stryder) Clarke This function sets the default value of the key to a boolean.
ChangeRegistryInt - The following methods return or set the default key values

Writen by Dale (stryder) Clarke This function sets the default value of the key to a integer.
ChangeRegistryStr - Writen by Dale (stryder) Clarke This function sets the default value of the key to a string.
DeleteRegKey - Deletes a key with and subkeys on win95

Writen by Dale (stryder) Clarke On Win 95 this removes all subkeys but on NT the key is only removed if it has NO subkeys.
GetRegistryBool - Writen by Dale (stryder) Clarke This function returns a boolean value for the given key if the key does not exist the key will be created with the default value.
GetRegistryInt - Writen by Dale (stryder) Clarke This function returns a integer value for the given key if the key does not exist the key will be created with the default value.
GetRegistryStr - Writen by Dale (stryder) Clarke This function returns a string value for the given key if the key does not exist the key will be created with the default value.
GetRegSubTree - Sets a stringlist with all subkeys

Writen by Dale (stryder) Clarke This function returns a stringlist of names of a valid registration key.
LoadBoolFromRegistry - Writen by Dale (stryder) Clarke This function returns a boolean from a registry key.
LoadIntFromRegistry - Writen by Dale (stryder) Clarke This function returns a integer from a registry key.
LoadStrFromRegistry - Writen by Dale (stryder) Clarke This function returs a string from a registry key.
NTDeleteRegKey - Deletes a key with and subkeys on win95/NT

Writen by Dale (stryder) Clarke This function is extreemly dangerous.
RegistryKeyExists - ********************************************************** The following method was written by Almer.
SaveBoolToRegistry - Writen by Dale (stryder) Clarke This function saves a boolean to a registry key.
SaveIntToRegistry - Notes Mainkey must be one of the following root registry keys eg.
SaveStrToRegistry - Writen by Dale (stryder) Clarke This function saves a string to a registry key.

Types

Constants

Variables


Functions


procedure ChangeRegistryBool( mainKey:longint; aKey : String; aValue : boolean );

Writen by Dale (stryder) Clarke This function sets the default value of the key to a boolean.

procedure ChangeRegistryInt( mainKey:longint; aKey : String; aValue : longint );

The following methods return or set the default key values

Writen by Dale (stryder) Clarke This function sets the default value of the key to a integer.


procedure ChangeRegistryStr( mainKey:longint; aKey : String; aValue : String );

Writen by Dale (stryder) Clarke This function sets the default value of the key to a string.

function DeleteRegKey(MainKey:longint;aKey:string):boolean;

Deletes a key with and subkeys on win95

Writen by Dale (stryder) Clarke On Win 95 this removes all subkeys but on NT the key is only removed if it has NO subkeys. So always call NTDeleteRegKey so if this fails it will recursively remove the keys.


function GetRegistryBool( MainKey:longint;aKey : String; Default :boolean ):boolean;

Writen by Dale (stryder) Clarke This function returns a boolean value for the given key if the key does not exist the key will be created with the default value.

function GetRegistryInt( mainKey:longint;aKey : String; Default : Integer ) : Integer;

Writen by Dale (stryder) Clarke This function returns a integer value for the given key if the key does not exist the key will be created with the default value.

function GetRegistryStr( mainKey:longint; aKey : String; Default : String ):string;

Writen by Dale (stryder) Clarke This function returns a string value for the given key if the key does not exist the key will be created with the default value.

function GetRegSubTree( MainKey : Longint; var aList : TStringList; aKey : string ) : Boolean;

Sets a stringlist with all subkeys

Writen by Dale (stryder) Clarke This function returns a stringlist of names of a valid registration key. You can yous this to recursivly look for more subkeys. Remember to always pass a initialized stringlist and to free it yourself.


function LoadBoolFromRegistry(MainKey:longint;RegistryKey,aItem:string;aValue:boolean):boolean;

Writen by Dale (stryder) Clarke This function returns a boolean from a registry key.

function LoadIntFromRegistry(MainKey:longint;RegistryKey,aItem:string;aValue:integer):integer;

Writen by Dale (stryder) Clarke This function returns a integer from a registry key.

function LoadStrFromRegistry(MainKey:longint;RegistryKey,aItem:string;aValue:string):string;

Writen by Dale (stryder) Clarke This function returs a string from a registry key.

function NTDeleteRegKey(MainKey:longint;const aKey:string):boolean;

Deletes a key with and subkeys on win95/NT

Writen by Dale (stryder) Clarke This function is extreemly dangerous. The key specified and all subkeys WILL be removed. Especially DO NOT pass the string SOFTWARE or anyother important registry root folder. On NT RegDeleteKey will not remove a key if it has subkeys. This function will remove all sukeys on NT


function RegistryKeyExists(Mainkey:longint;aKey:string): boolean;

********************************************************** The following method was written by Almer.S. Tigelaar . I have included it because I don't see any other way to check a key with out creating it.

function SaveBoolToRegistry(MainKey:longint;RegistryKey,aItem:string;aValue:boolean):boolean;

Writen by Dale (stryder) Clarke This function saves a boolean to a registry key.

function SaveIntToRegistry(MainKey:longint;RegistryKey,aItem:string;aValue:integer):boolean;

Notes Mainkey must be one of the following root registry keys eg. HKEY_LOCAL_MACHINE HKEY_CURRENT_USER ect. aKey and aItem are the registry nodes you wish to reference eg. akey := '/SOFTWARE/MySoftware/Window Position'; This string must begin with a slash and end with no slash. aValue and aDefault is the value of the key you wish to change. aList is a tStringlist that you must create before calling the method and you must free after calling the method. aList will return containing the names of all subkeys of aKey. eg. procedure MyWindow.LoadSize; var MyList:tStringlist; i:integer; const MyKey:string='/SOFTWARE/Your Software/MyWindow Size'; RootKey:Longint=HKEY_CURRENT_USER; begin MuList:=tstringList.create; if GetRegSubTree(RootKey,MyList,MyKey) then begin for i:=0 to pred(MyList.count) do begin if MyList[i] = 'Left' then SaveIntToRegistry(RootKey,MyKey+'/'+MyList[i],Left); if MyList[i] = 'Right' then SaveIntToRegistry(RootKey,MyKey+'/'+MyList[i],Right); if MyList[i] = 'Top' then SaveIntToRegistry(RootKey,MyKey+'/'+MyList[i],Top); if MyList[i] = 'Height' then SaveIntToRegistry(RootKey,MyKey+'/'+MyList[i],Height); end; end; MyList.Free; end; procedure MyWindow.SaveSize; var MyList:tStringlist; i:integer; const MyKey:string='/SOFTWARE/Your Software/MyWindow Size'; RootKey:Longint=HKEY_CURRENT_USER; begin MuList:=tstringList.create; GetRegSubTree(RootKey,MyList,MyKey); if MyList.count > 0 then begin for i:=0 to pred(MyList.count) do begin if MyList[i] = 'Left' then Left:=LoadIntToRegistry(RootKey,MyKey+'/'+MyList[i],Left); if MyList[i] = 'Right' then Right:=LoadIntToRegistry(RootKey,MyKey+'/'+MyList[i],Right); if MyList[i] = 'Top' then Top:=LoadToRegistry(RootKey,MyKey+'/'+MyList[i],Top); if MyList[i] = 'Height' then Height:=LoadIntToRegistry(RootKey,MyKey+'/'+MyList[i],Height); end; end; MyList.Free; end; You do not have to enumerate the keys but I thought the above two examples would help if you wish to for some reason. The above examples could alse be safely done with procedure MyWindow.LoadSize; var MyKey:String; RootKey:Longint; Begin MyKey:='/SOFTWARE/Your Software/MyWindow Size'; RootKey:=HKEY_CURRENT_USER; SaveIntToRegistry(RootKey,MyKey+'/Left',Left); SaveIntToRegistry(RootKey,MyKey+'/Right',Right); SaveIntToRegistry(RootKey,MyKey+'/Top',Top); SaveIntToRegistry(RootKey,MyKey+'/Height',Height); end; procedure MyWindow.SaveSize; var MyKey:String; RootKey:Longint; Begin MyKey:='/SOFTWARE/Your Software/MyWindow Size'; RootKey:=HKEY_CURRENT_USER; Left:=LoadIntToRegistry(RootKey,MyKey+'/Left',Left); Right:=LoadIntToRegistry(RootKey,MyKey+'/Right',Right); Top:=LoadToRegistry(RootKey,MyKey+'/Top',Top); Height:=LoadIntToRegistry(RootKey,MyKey+'/Height',Height); end; The Save and Change methods will create the key and set its values if they do not exists. The Load methods will return the value of aValue if the key does not exists. The GetRegSubTree will return false if aKey does not exists. If you have any questions about using the registry in Delphi you can contact me through EMail at the following: stryder@.multiboard.com or stryder@bbs.multiboard.com or my Web page at http://www.multiboard.com/~stryder

Writen by Dale (stryder) Clarke This function saves a string to a registry key.


function SaveStrToRegistry(MainKey:longint;RegistryKey,aItem:string;aValue:string):boolean;

Writen by Dale (stryder) Clarke This function saves a string to a registry key.

Types


Constants


Variables