Solved

Copy a record to and from a "buffer" (array of AnsiChar) in Delphi

Posted on 2010-08-17
4
939 Views
Last Modified: 2013-11-23
Hello,

In Delphi, I have some packed records that I'm trying to pass through a socket for convenience to reduce the amount of parsing I need to do.

For example
TPacketHeader = packed record
  blah: shortstring;
  something: cardinal;
end;

I want to both write and then read this from a 'buffer'
e.g. Buffer: array[0..XXXX] of AnsiChar;

How can I do that without parsing each field manually?  
0
Comment
Question by:bbbbbbbbbbbbbbbbbbb
  • 3
4 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 33461080
I haven't tested this yet but try it
var
  Stream: TStringStream;

  FillChar(PacketHeader, Sizeof(PacketHeader), #0);
  add values to PacketHeader
  Stream := TStringStream.Create('');
  try
    Stream.WriteBuffer(PacketHeader, SizeOf(PacketHeader));
    Stream.Seek(0, soFromBeginning);
    Stream.ReadBuffer(Buffer, Stream.Size);
  finally
     FreeAndNil(Stream);
  end;

to read it
var
  Stream: TStringStream;
begin
  Stream := TStringStream.Create('');
  try
     Stream.WriteBuffer(Buffer, SizeOf(Buffer));
     Stream.Seek(0, soFromBeginning);
     Stream.ReadBuffer(Rec, SizeOf(Rec));
     showmessage(rec.name);
  finally
  FreeAndNil(stream);
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 33461091
Hit the enter key by mistake
To read
var
  Stream: TStringStream;
begin
  Stream := TStringStream.Create('');
  try
     Stream.WriteBuffer(Buffer, SizeOf(Buffer));
     Stream.Seek(0, soFromBeginning);
     Stream.ReadBuffer(PacketHeader, SizeOf(PacketHeader));
  finally
    FreeAndNil(stream);
  end;
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 33461098
You could also change your HeaderRecord into a TPersistent class
Publish its properties and use delphi's streaming method
0
 
LVL 13

Accepted Solution

by:
aflarin earned 50 total points
ID: 33462010
procedure TForm1.Button1Click(Sender: TObject);
var
  Buffer: array[0..sizeof(TPacketHeader)] of AnsiChar;
  Source, Dest: TPacketHeader;
begin
  // init source
  Source.blah:= 'BlahBlahBlah';
  Source.something:= 127;

  // write to buffer
  Move( Source, Buffer, sizeof(TPacketHeader) );

  // read from buffer
  Move( Buffer, Dest, sizeof(TPacketHeader) );

  // test
  ShowMessage( Format('%s'#13#10'%d', [Dest.blah, Dest.something]) );

end;
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse find/step into stored procedure 3 183
dangers of teaching programming 10 130
Anomalies in ASP.NET ConnectionStrings 4 82
Eclipse Neon start with Admin account only 6 123
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

785 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