Solved

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

Posted on 2010-08-17
4
922 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Visual Studio 2013 5 68
error when i compile 5 483
Aptana 3.6 Preview Problem On Mac 35 97
Unable to open debugger port in Intellij idea 6 79
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
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.
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now