Einzelnen Beitrag anzeigen

Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#11

Re: Kapseln von Registryzugriffen

  Alt 21. Sep 2009, 15:54
Keine Ahnung woran es gelegen hat. Aber das Konzept war sowieso nicht schön. Hier mal meine Version:
Delphi-Quellcode:
unit AdvoPlex_Object;

interface

uses
  Windows,
  Classes,
  SysUtils,
  Registry;

const
  ADVOPATH = 'Software\AdvoSolutions\AdvoPlex';

type
  TAdvoPlex = class(TObject)
  private
    FUserSU: string;
    function CurrentUser: string;
    function GetUserSU: string;
    procedure SetUserSU(Value: string);
  public
    constructor Create;
    destructor Destroy; override;
  published
    property UserSU: string read GetUserSu write SetUserSU;
    procedure SaveUserSU;
    procedure LoadUserSU;
  end;

implementation

constructor TAdvoPlex.Create;
begin
  inherited Create;
end;

destructor TAdvoPlex.Destroy;
begin
  inherited Destroy;
end;

function TAdvoPlex.GetUserSU: string;
begin
  Result := FUserSU;
end;

procedure TAdvoPlex.LoadUserSU;
var
  reg: TRegistry;
begin
  reg := TRegistry.Create(KEY_READ);
  try
    reg.RootKey := HKEY_CURRENT_USER;
    if reg.OpenKeyReadOnly(ADVOPATH) then
    begin
      FUserSU := reg.ReadString('User');
      reg.CloseKey;
    end
    else
      raise Exception.CreateFmt('%s'+#13#10+'%s', ['Fehler beim Lesen von UserSU', SysErrorMessage(GetLastError)]);
  finally
    reg.Free;
  end;
end;

procedure TAdvoPlex.SaveUserSU;
var
  reg: TRegistry;
begin
  reg := TRegistry.Create(KEY_ALL_ACCESS);
  try
    reg.RootKey := HKEY_CURRENT_USER;
    if reg.OpenKey(ADVOPATH, True) then
    begin
      reg.WriteString('User', FUserSU);
      reg.CloseKey;
    end
    else
      raise Exception.CreateFmt('%s'+#13#10+'%s', ['Fehler beim Schreiben von UserSU', SysErrorMessage(GetLastError)]);
  finally
    reg.Free;
  end;
end;

procedure TAdvoPlex.SetUserSU(Value: string);
begin
  FUserSU := Value;
end;

function TAdvoPlex.CurrentUser: string;
var
  Buffer : array[0..MAX_PATH] of Char;
  Size : DWORD;
begin
  Size := SizeOf(Buffer);
  GetUserName(Buffer, Size);
  SetString(Result, Buffer, lstrlen(Buffer));
end;

end.
Und die Verwendung:
Delphi-Quellcode:
procedure TForm1.FormCreate(Sender: TObject);
begin
  AdvoPlex := TAdvoPlex.Create;
  try
    try
      AdvoPlex.LoadUserSU;
      ShowMessage(AdvoPlex.UserSu);
      AdvoPlex.UserSU := 'Test01';
      AdvoPlex.SaveUserSU;
    except
      on E: Exception do
        ShowMessage(e.Message);
    end;
  finally
    AdvoPlex.Free;
  end;
end;
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat