Solved

TRegistry.WriteBinaryData

Posted on 1998-07-11
4
455 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
Comment Utility

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

Author Comment

by:Bernt
Comment Utility
Edited text of question
0
 
LVL 3

Accepted Solution

by:
vladika earned 100 total points
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now