Using Dynamic Records with TMemoryStream

I am using TMemoryStream to send dynamic records using indy TCPServer.

The structure of my record is as follows:

TMyRecord = record
  name : string ;  // unassigned size
  ID : integer ;
  Picture : TJPEGImage ;  
end;

How can I assign the variable of this record type into a TMemoryStream? Also please explain me how to extract this TMemoryStream into record type.

Answers


The basic idea is

Tmyrecord = Record
  name : string ;  // unassaigned 
  ID : integer ;
  Picture : TJPEGImage ;
  procedure SaveToStream(const aStream: TStream);
  procedure LoadFromStream(const aStream: TStream);
end;

procedure Tmyrecord.SaveToStream(const aStream: TStream);
var Len: Integer;
begin
  // save the length of name str
  Len := Length(name);
  aStream.Write(Len, SizeOf(Len));
  // save the name
  if(Len > 0)then aStream.Write(name[1], Len * SizeOf(name[1]));
  // save the ID
  aStream.Write(ID, SizeOf(ID));
  // save image
  Picture.SaveToStream(aStream);
end;

procedure Tmyrecord.LoadFromStream(const aStream: TStream);
var Len: Integer;
begin
  // read the length of name str
  aStream.Read(Len, SizeOf(Len));
  if(Len > 0)then begin
     // read the name
     SetLength(name, Len);
     aStream.Read(name[1], Len * SizeOf(name[1]));
  end else name := '';
  // load the ID
  aStream.Read(ID, SizeOf(ID));
  // load image
  if(Picture = nil)then Picture := TJPEGImage.Create;
  Picture.LoadFromStream(aStream);
end;

Need Your Help

How do I use Scipy minimize with constraints and dynamic function

python optimization numpy matrix scipy

I am new to scipy.optimize module and need help trying to use the minimize function on a formula V that works with matrixes and has 2 constraints but I am not sure if I am handling the formation of...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.