Solved

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

Posted on 2010-08-17
4
959 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
[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
 
LVL 32

Expert Comment

by:Ephraim Wangoya
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:Ephraim Wangoya
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:Ephraim Wangoya
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses

738 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