?
Solved

TRegistry.WriteBinaryData

Posted on 1998-07-11
4
Medium Priority
?
483 Views
Last Modified: 2011-09-20
I fail understand how to use this thingy.

I understand using eg;
Type
TWriteToReg = record
  str: string;
  i: integer;
end;

But:
Type
TWriteToReg = record
  Font: TFont;
end;

How do I:
var
  f: WriteToReg;
begin
  f.Font:=TFont(aDefinedFont);
  registryInitialized.WriteBinaryData('Font', f, sizeOf(f));
end;

How should this be done correct?
How do I then restore it with ReadBinaryData correct?
0
Comment
Question by:Bernt
  • 3
4 Comments
 

Author Comment

by:Bernt
ID: 1357701

Best regards and so on... /Bernt Levinsson
http://w1.321.telia.com/~u32102551/
0
 

Author Comment

by:Bernt
ID: 1357702
Edited text of question
0
 
LVL 3

Accepted Solution

by:
vladika earned 300 total points
ID: 1357703
Variable of Object type internally presents as pointer.
So, what do you want? Save pointer into registry?
Why?

If you want save object into registry then
see my PAQ Q.10058990

From PAQ:

Create wrapper class as
type
  TWrapper = class(TComponent)
  private
    FItem: TPersistent;
  published
    property Item: TPersistent read FItem write FItem;
  end;

Now you can store any object inherited from TPersistent.
For example
  Wrapper.Item := Form.Font;
  Stream.WriteComponent(Wrapper);
or
  Wrapper.Item := Form.Font;
  Stream.ReadComponent(Wrapper);

Here is full example
type
  TForm1 = class(TForm)
    WriteFontBtn: TButton;
    ReadFontBtn: TButton;
    procedure WriteFontBtnClick(Sender: TObject);
    procedure ReadFontBtnClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TWrapper = class(TComponent)
  private
    FItem: TPersistent;
  published
    property Item: TPersistent read FItem write FItem;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

const RegPath = 'Software\Test\Properties';

procedure TForm1.WriteFontBtnClick(Sender: TObject);
var Stream: TMemoryStream;
    Wrapper: TWrapper;
    Reg: TRegistry;
    Memory: Pointer;
begin
  Wrapper := TWrapper.Create(nil);
  try
    Stream := TMemoryStream.Create;
    try
      Wrapper.Item := Font;  // set reference on Form1.Font
      Stream.WriteComponent(Wrapper); // write Form1.Font into Stream
      Reg := TRegistry.Create;
      try
        Reg.OpenKey(RegPath, True);
        Memory := Stream.Memory;
        Reg.WriteBinaryData('Font', Memory^, Stream.Size);  // write Form1.Font into registry
      finally
        Reg.Free;
      end;
    finally
      Stream.Free;
    end;
  finally
    Wrapper.Free;
  end;
end;

procedure TForm1.ReadFontBtnClick(Sender: TObject);
var Stream: TMemoryStream;
    Wrapper: TWrapper;
    Reg: TRegistry;
    Memory: Pointer;
begin
  Wrapper := TWrapper.Create(nil);
  try
    Stream := TMemoryStream.Create;
    try
      Reg := TRegistry.Create;
      try
        Reg.OpenKey(RegPath, True);
        Stream.Size := Reg.GetDataSize('Font');  // get binary data size
        Memory := Stream.Memory;
        Reg.ReadBinaryData('Font', Memory^, Stream.Size); // read binary data into Stream
      finally
        Reg.Free;
      end;
      Wrapper.Item := Font;  // set reference on Form1.Font
      Stream.ReadComponent(Wrapper); // read Form1.Font
    finally
      Stream.Free;
    end;
  finally
    Wrapper.Free;
  end;
end;
0
 

Author Comment

by:Bernt
ID: 1357704
It look nice, better then using the TReader I was exprimenting with for restoring a saved Component(I did probebly everything wrong beocuse I wanted to read/write Object).

I want to store a few objects that has some "Sets" in the properties. Some objects has a huge amount of "Sets" so this would be nicer if I can store it like this. For the moment I need to store "none Components", only "Objects" without owner(some private declared items used for the get values when the "user design the application". And before .Free the items I store those "user designed" items.

So, now I'll copy/paste your code and hack around and so where I end up.
Thanks a million!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question