?
Solved

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

Posted on 2010-08-17
4
Medium Priority
?
977 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 200 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
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 column That will then direct you to their download page. From that p…
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 use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

801 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