[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2010-08-17
4
Medium Priority
?
1,007 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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: …
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses

649 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