?
Solved

TRegistry.WriteBinaryData

Posted on 1998-07-11
4
Medium Priority
?
475 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

777 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