Solved

TRegistry.WriteBinaryData

Posted on 1998-07-11
4
458 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 100 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 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