Board index » delphi » regitryproblem

regitryproblem

Hi
Im a pretty new delphi user and I want to learn more about accessing the
windows system registry from within delphi. My first problem is that I
simply cannot get a value from the registry into a label. Im sure this is a
very simple problem to solve, but I dont know how to.
Please tell me what errors Ive made.
Here is the code:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Registry;

type
  TRegistry1 = class(TRegistry)
  end;
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
function GetRegKey(const WantedPath, WantedKey : string) : string;
var Registry:TRegistry;
begin
result:='';
Registry:=TRegistry.Create;
try
Registry.RootKey:=HKEY_CURRENT_USER;
result:=Registry.ReadString(WantedKey);
finally
TRegistry.Free;
end;
end;
begin
  label1.caption:=GetRegKey('ControlPanel\Desktop','Wallpaper');
end;

end.

Thanks for any help!

 

Re:regitryproblem


Hello Johan.

You have made 3 errors. First of all, you have deleted too much of the
GetRegKey-Function I send to you two weeks ago. Before you read the
registry-key you must set the registry-path. It should read:

<snip>
if Registry.OpenKey(WantedPath,False) then
   result:=Registry.ReadString(WantedKey);
<snip>

Second, in the finally-section you must write Registry.Free, not
TRegistry.Free. You can't free memory of a class-definition, only of an
instance.

The third error is simply a mispelling: The path for the 'Wallpaper'-key
is
'Control Panel\Desktop' (with blank!), not 'ControlPanel\Desktop'.

Hope, I could help,
   Klaus

Re:regitryproblem


On Sun, 22 Nov 1998 21:41:47 +0100, "Johan Lundahl"

Quote
<jo...@earthdome.com> wrote:
>Hi
>Im a pretty new delphi user and I want to learn more about accessing the
>windows system registry from within delphi. My first problem is that I
>simply cannot get a value from the registry into a label. Im sure this is a
>very simple problem to solve, but I dont know how to.
>Please tell me what errors Ive made.
>Here is the code:

>unit Unit1;

>interface

>uses
>  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
>  StdCtrls, Registry;

>type
>  TRegistry1 = class(TRegistry)
>  end;
>  TForm1 = class(TForm)
>    Button1: TButton;
>    Label1: TLabel;
>    procedure Button1Click(Sender: TObject);
>  private
>    { Private declarations }
>  public
>    { Public declarations }
>  end;

>var
>  Form1: TForm1;

>implementation

>{$R *.DFM}

>procedure TForm1.Button1Click(Sender: TObject);
>function GetRegKey(const WantedPath, WantedKey : string) : string;
>var Registry:TRegistry;
>begin
>result:='';
>Registry:=TRegistry.Create;
>try
>Registry.RootKey:=HKEY_CURRENT_USER;

 You don't call OpenKey here, so add e.g.

  Registry.OpenKey(WantedPath, false);

Quote
>result:=Registry.ReadString(WantedKey);
>finally
>TRegistry.Free;
>end;
>end;
>begin
>  label1.caption:=GetRegKey('ControlPanel\Desktop','Wallpaper');

You probably want
   label1.caption:=GetRegKey('Control Panel\Desktop','Wallpaper');
(with an extra space)

Also, before you decide there is an error, are you sure there is a
Wallpaper set ?

hth
David

Quote
>end;

>end.

>Thanks for any help!

------------------
David A. Schweizer

iec ProGAMMA, The Netherlands
d.a.schweizer[OK, i don't want any more spam]gamma.rug.nl
guess where the '@' goes ?

Re:regitryproblem


On Sun, 22 Nov 1998 21:41:47 +0100, "Johan Lundahl"

Quote
<jo...@earthdome.com> wrote:
>Hi
>Im a pretty new delphi user and I want to learn more about accessing the
>windows system registry from within delphi. My first problem is that I
>simply cannot get a value from the registry into a label. Im sure this is a
>very simple problem to solve, but I dont know how to.
>Please tell me what errors Ive made.
>Here is the code:

>procedure TForm1.Button1Click(Sender: TObject);
>function GetRegKey(const WantedPath, WantedKey : string) : string;
>var Registry:TRegistry;
>begin
>result:='';
>Registry:=TRegistry.Create;
>try
>Registry.RootKey:=HKEY_CURRENT_USER;
>result:=Registry.ReadString(WantedKey);

You're reading from the root key. Open the Wantedpath-Key first:
Registry.OpenKey(WantedPath,false);

- Ulli -

The Coder's Knowledge Base
http://www.netalive.org/ckb

Other Threads