home *** CD-ROM | disk | FTP | other *** search
- { Newsgroups: comp.lang.pascal.delphi.components
- Subject: TMegaRegistry (v0.01=E1 code)
- From: christian.tiberg@silver.ct.se (Christian Tiberg)
- Date: Mon, 07 Aug 95 23:33:00 +0200
-
- Hello All!
-
- Below is the code for a component that can use a .INI file or the Windows
- Registry to store the data an application needs to save. It is as of yet in a
- very early stage, but I've tested it quite a bit and it seems stable. Any
- suggestions/bug reports welcome! The Mega in the name isn't because I'm so
- great, but the company I work for is named MegaComm :) }
-
- unit Megareg;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Classes,
- ShellAPI, IniFiles;
-
- type
- TWindowsRegistry = class(TIniFile)
- private
- { Private declarations }
- AppName, Company: string;
- Key: HKey;
- protected
- { Protected declarations }
- public
- { Public declarations }
- constructor Create(const CompanyName, ApplicationName: string);
- destructor Destroy; override;
-
- function ReadString(const Section, Ident, Default: string): string;
- function ReadInteger(const Section, Ident: string; Default: longint):
- longint;
- function ReadBool(const Section, Ident: string; Default: boolean):
- boolean;
-
- procedure WriteString(const Section, Ident, Value: string);
- procedure WriteInteger(const Section, Ident: string; Value: longint);
- procedure WriteBool(const Section, Ident: string; Value: boolean);
-
- procedure ReadSection(const Section: string; Strings: TStrings);
- procedure ReadSectionValues(const Section: string; Strings: TStrings);
- procedure EraseSection(const Section: string);
- end;
-
- TMRPreference = (mrpAuto, mrpIni, mrpRegistry);
- TMegaRegistry = class(TComponent)
- private
- FApp, FCompany, FIniName: string;
- FPreference: TMRPreference;
- MyStorage: TObject;
- StoredAs: TMRPreference;
-
- procedure SetApp(AValue: string);
- procedure SetCompany(AValue: string);
- procedure SetIniName(AValue: string);
- procedure SetPreference(AValue: TMRPreference);
- protected
- procedure Loaded; override;
- public
- destructor Destroy; override;
-
- procedure Close;
- procedure Open;
-
- function ReadString(const Section, Ident, Default: string): string;
- function ReadInteger(const Section, Ident: string; Default: longint):
- longint;
- function ReadBool(const Section, Ident: string; Default: boolean):
- boolean;
-
- procedure WriteString(const Section, Ident, Value: string);
- procedure WriteInteger(const Section, Ident: string; Value: longint);
- procedure WriteBool(const Section, Ident: string; Value: boolean);
-
- procedure ReadSection(const Section: string; Strings: TStrings);
- procedure ReadSectionValues(const Section: string; Strings: TStrings);
- procedure EraseSection(const Section: string);
- published
- property App: string read FApp write SetApp;
- property Company: string read FCompany write SetCompany;
- property IniName: string read FIniName write SetIniName;
- property Preference: TMRPreference read FPreference write SetPreference;
- end;
-
- procedure Register;
-
- implementation
-
- procedure Register;
- begin
- RegisterComponents('New', [TMegaRegistry]);
- end;
-
- constructor TWindowsRegistry.Create(const CompanyName, ApplicationName:
- string); var
- tmp: array[1..256] of char;
- begin
- AppName := ApplicationName;
- Company := CompanyName;
- if RegCreateKey(HKEY_CLASSES_ROOT, StrPCopy(@tmp, Company + '\' + AppName),
- Key) <> ERROR_SUCCESS then
- Key := 0;
- end;
-
- destructor TWindowsRegistry.Destroy;
- begin
- if Key <> 0 then
- RegCloseKey(Key);
- end;
-
- function TWindowsRegistry.ReadString(const Section, Ident, Default: string):
- string; var
- tmp, Buff: array[0..255] of char;
- cb: longint;
- begin
- cb := 256;
- if RegQueryValue(Key, StrPCopy(tmp, Section + '\' + Ident), Buff, cb) =
- ERROR_SUCCESS then
- ReadString := StrPas(Buff)
- else
- ReadString := Default;
- end;
-
- function TWindowsRegistry.ReadInteger(const Section, Ident: string; Default:
- longint): longint; var
- tmp, Buff: array[0..255] of char;
- cb: longint;
- begin
- cb := 256;
- if RegQueryValue(Key, StrPCopy(tmp, Section + '\' + Ident), Buff, cb) =
- ERROR_SUCCESS then
- ReadInteger := StrToInt(StrPas(Buff))
- else
- ReadInteger := Default;
- end;
-
- function TWindowsRegistry.ReadBool(const Section, Ident: string; Default:
- boolean): boolean; var
- tmp, Buff: array[0..255] of char;
- cb: longint;
- begin
- cb := 256;
- if RegQueryValue(Key, StrPCopy(tmp, Section + '\' + Ident), Buff, cb) =
- ERROR_SUCCESS then
- ReadBool := (StrToInt(StrPas(Buff)) <> 0)
- else
- ReadBool := Default;
- end;
-
- procedure TWindowsRegistry.WriteString(const Section, Ident, Value: string);
- var
- tmp, Buff: array[0..255] of char;
- cb: longint;
- begin
- cb := Length(Value);
- RegSetValue(Key, StrPCopy(tmp, Section + '\' + Ident), REG_SZ,
- StrPCopy(Buff, Value), cb); end;
-
- procedure TWindowsRegistry.WriteInteger(const Section, Ident: string; Value:
- longint); var
- tmp, Buff: array[0..255] of char;
- cb: longint;
- AStr: string;
- begin
- AStr := IntToStr(Value);
- cb := Length(AStr);
- RegSetValue(Key, StrPCopy(tmp, Section + '\' + Ident), REG_SZ,
- StrPCopy(Buff, AStr), cb); end;
-
- procedure TWindowsRegistry.WriteBool(const Section, Ident: string; Value:
- boolean); var
- tmp, Buff: array[0..255] of char;
- cb: longint;
- AStr: string;
- begin
- if Value then
- AStr := '1'
- else
- AStr := '0';
- cb := Length(AStr);
- RegSetValue(Key, StrPCopy(tmp, Section + '\' + Ident), REG_SZ,
- StrPCopy(Buff, AStr), cb); end;
-
- procedure TWindowsRegistry.ReadSection(const Section: string; Strings:
- TStrings); var
- TmpKey: HKey;
- Buff, Verde: array[1..256] of char;
- Index, Resultat, cb: longint;
- begin
- Strings.Clear;
- if RegOpenKey(Key, StrPCopy(@Buff, Section), TmpKey) <> ERROR_SUCCESS then
- exit;
- Index := 0;
- Resultat := RegEnumKey(TmpKey, Index, @Buff, 256);
- while Resultat = ERROR_SUCCESS do
- begin
- Strings.Add(StrPas(@Buff));
- inc(Index);
- Resultat := RegEnumKey(TmpKey, Index, @Buff, 256);
- end;
- RegCloseKey(TmpKey);
- end;
-
- procedure TWindowsRegistry.ReadSectionValues(const Section: string; Strings:
- TStrings); var
- TmpKey: HKey;
- Buff, Verde: array[1..256] of char;
- Index, Resultat, cb: longint;
- st: string;
- begin
- Strings.Clear;
- if RegOpenKey(Key, StrPCopy(@Buff, Section), TmpKey) <> ERROR_SUCCESS then
- exit;
- Index := 0;
- Resultat := RegEnumKey(TmpKey, Index, @Buff, 256);
- while Resultat = ERROR_SUCCESS do
- begin
- cb := 256;
- RegQueryValue(TmpKey, @Buff, @Verde, cb);
- st := StrPas(@Verde);
- if st <> '' then
- Strings.Add(StrPas(@Buff) + '=' + st);
- inc(Index);
- Resultat := RegEnumKey(TmpKey, Index, @Buff, 256);
- end;
- RegCloseKey(TmpKey);
- end;
-
- procedure TWindowsRegistry.EraseSection(const Section: string); var
- Buff: array[1..256] of char;
- begin
- RegDeleteKey(Key, StrPCopy(@Buff, Section));
- end;
-
- procedure TMegaRegistry.SetApp(AValue: string);
- begin
- Close;
- FApp := AValue;
- end;
-
- procedure TMegaRegistry.SetCompany(AValue: string);
- begin
- Close;
- FCompany := AValue;
- end;
-
- procedure TMegaRegistry.SetIniName(AValue: string);
- begin
- Close;
- FIniName := AValue;
- end;
-
- procedure TMegaRegistry.SetPreference(AValue: TMRPreference); begin
- Close;
- FPreference := AValue;
- end;
-
- procedure TMegaRegistry.Loaded;
- begin
- inherited Loaded;
- if not (csDesigning in ComponentState) then
- Open;
- end;
-
- destructor TMegaRegistry.Destroy;
- begin
- Close;
- inherited Destroy;
- end;
-
- procedure TMegaRegistry.Close;
- begin
- if MyStorage <> nil then
- if StoredAs = mrpIni then
- (MyStorage as TIniFile).Free
- else
- (MyStorage as TWindowsRegistry).Free;
- MyStorage := nil;
- end;
-
- procedure TMegaRegistry.Open;
- var
- ver, maj, min: longint;
- begin
- Close;
- if (App = '') or (Company = '') or (IniName = '') then
- exit;
- if Preference = mrpAuto then
- begin
- ver := (GetVersion and $FFFF);
- maj := ver and $FF;
- min := (ver and $FF00) shr 8;
- if (maj > 3) or ((maj = 3) and (min > 20)) then
- StoredAs := mrpRegistry
- else
- StoredAs := mrpIni;
- end
- else
- StoredAs := Preference;
- if StoredAs = mrpIni then
- begin
- if IniName = '' then
- exit;
- MyStorage := TIniFile.Create(IniName);
- end
- else
- begin
- if (App = '') or (Company = '') then
- exit;
- MyStorage := TWindowsRegistry.Create(Company, App);
- end;
- end;
-
- function TMegaRegistry.ReadString(const Section, Ident, Default: string):
- string; begin
- if StoredAs = mrpIni then
- ReadString := (MyStorage as TIniFile).ReadString(Section, Ident, Default)
- else
- ReadString := (MyStorage as TWindowsRegistry).ReadString(Section, Ident,
- Default); end;
-
- function TMegaRegistry.ReadInteger(const Section, Ident: string; Default:
- longint): longint; begin
- if StoredAs = mrpIni then
- ReadInteger := (MyStorage as TIniFile).ReadInteger(Section, Ident,
- Default)
- else
- ReadInteger := (MyStorage as TWindowsRegistry).ReadInteger(Section,
- Ident, Default); end;
-
- function TMegaRegistry.ReadBool(const Section, Ident: string; Default:
- boolean): boolean; begin
- if StoredAs = mrpIni then
- ReadBool := (MyStorage as TIniFile).ReadBool(Section, Ident, Default)
- else
- ReadBool := (MyStorage as TWindowsRegistry).ReadBool(Section, Ident,
- Default); end;
-
- procedure TMegaRegistry.WriteString(const Section, Ident, Value: string); begin
- if StoredAs = mrpIni then
- (MyStorage as TIniFile).WriteString(Section, Ident, Value)
- else
- (MyStorage as TWindowsRegistry).WriteString(Section, Ident, Value) end;
-
- procedure TMegaRegistry.WriteInteger(const Section, Ident: string; Value:
- longint); begin
- if StoredAs = mrpIni then
- (MyStorage as TIniFile).WriteInteger(Section, Ident, Value)
- else
- (MyStorage as TWindowsRegistry).WriteInteger(Section, Ident, Value) end;
-
- procedure TMegaRegistry.WriteBool(const Section, Ident: string; Value:
- boolean); begin
- if StoredAs = mrpIni then
- (MyStorage as TIniFile).WriteBool(Section, Ident, Value)
- else
- (MyStorage as TWindowsRegistry).WriteBool(Section, Ident, Value); end;
-
- procedure TMegaRegistry.ReadSection(const Section: string; Strings: TStrings);
- begin
- if StoredAs = mrpIni then
- (MyStorage as TIniFile).ReadSection(Section, Strings)
- else
- (MyStorage as TWindowsRegistry).ReadSection(Section, Strings); end;
-
- procedure TMegaRegistry.ReadSectionValues(const Section: string; Strings:
- TStrings); begin
- if StoredAs = mrpIni then
- (MyStorage as TIniFile).ReadSectionValues(Section, Strings)
- else
- (MyStorage as TWindowsRegistry).ReadSectionValues(Section, Strings); end;
-
- procedure TMegaRegistry.EraseSection(const Section: string); begin
- if StoredAs = mrpIni then
- (MyStorage as TIniFile).EraseSection(Section)
- else
- (MyStorage as TWindowsRegistry).EraseSection(Section); end;
-
- end.
-
- { Best regards,
- Christian // ctiberg@silver.ct.se }
-
-
-