Board index » delphi » Converting record data into variant array of varByte

Converting record data into variant array of varByte

I want to move some data records across a marshalled Ole automation boundry
as efficiently as possible:

record data:

var Resource, location:string15;
     ID:String2;
    Booking_ID:longint;
    duration,start:integer;
    tag:string1;

I want to put a bunch of these records into

var transfer_Array:variant;

transfer_Array:=vararrayCreate([0,length],varbyte);

I'm not sure of how to move the bytes that make up the fields in the records
into the transfer_array, and then on the receiving end to move them from the
array back into a set of similar record variables.

Could someone advise or point me towards some good examples of this in the
doc.
--
Thanks in advance for any help.
Sam...@bookitall.com

 

Re:Converting record data into variant array of varByte


On Wed, 27 May 1998 06:22:26 -0400, "Samuel Washburn"

Quote
<Sam...@bookitall.com> wrote:
>I want to move some data records across a marshalled Ole automation boundry
>as efficiently as possible:

>record data:

>var Resource, location:string15;
>     ID:String2;
>    Booking_ID:longint;
>    duration,start:integer;
>    tag:string1;

>I want to put a bunch of these records into

>var transfer_Array:variant;

>transfer_Array:=vararrayCreate([0,length],varbyte);

>I'm not sure of how to move the bytes that make up the fields in the records
>into the transfer_array, and then on the receiving end to move them from the
>array back into a set of similar record variables.

>Could someone advise or point me towards some good examples of this in the
>doc.

Sam,

I had a similar problem not so long ago. I couldn't find much
documentation on it either, but eventually (very eventually!) came up
with two *very* useful functions:

{*****************************************************}
function BytesToVariant(pBase: Pointer; iSize: LongInt): OleVariant;
type
  pPointer = ^Pointer;
var
  pDataOut: pPointer;
begin
  Result := VarArrayCreate([0, iSize], VarByte);
  pDataOut := VarArrayLock(Result);
  Move(pBase^, pDataOut^, iSize);
  VarArrayUnlock(Result);
end;

{*****************************************************}
procedure VariantToBytes(aVarArray: OleVariant; pBase: Pointer);
var
 DataIn: Pointer;
begin
  DataIn := VarArrayLock(aVarArray);

  Move(DataIn^, pBase^, VarArrayHighBound(aVarArray, 1));
  VarArrayUnlock(aVarArray);
end;

I've used these functions to pass data between a Client and a Server
on separate machines, e.g. to pass a record from the Server to the
Client, do something like

aOleVar := BytesToVariant(@MyRecord, SizeOf(TMyRecord)

where aOleVar is of type OleVariant. You can these pass the OleVariant
and use the VariantToBytes routine on the Client to reconstruct the
record.

Hope this helps,

John

Re:Converting record data into variant array of varByte


If you like to pass several  records at a time , maybe you should use a
TClientDataSet and its Data property.
Create a empty AClientdataset (using FieldsDefs), populate it with
records and send send along your interface the AClientDataSet.Data
property.
In the receiver, after retrieving the variant, assign to a
TClientDataSet the received data (AClientDataSet.Data:=ReceivedData)

Remus

Re:Converting record data into variant array of varByte


Quote
John Edward Scott wrote in message <356c0740.19218...@194.169.119.101>...
>e.g. to pass a record from the Server to the
>Client, do something like

>aOleVar := BytesToVariant(@MyRecord, SizeOf(TMyRecord)

>where aOleVar is of type OleVariant. You can these pass the OleVariant
>and use the VariantToBytes routine on the Client to reconstruct the
>record.

John, I like your method. Thanks.

For clarification of the above example, could you detail how you declare and
populate "Myrecord" in this example. Again many thanks. Hope I can return
the favor at some point. Sam.

Re:Converting record data into variant array of varByte


There's a good article on doing something similar to this in a past Delphi
Informant (2 or 3 months ago?), and there's sample code on their website -
see www.informant.com/delphi (or something similar).  The article took a
component from one app and used an Variant array of bytes to pass it to
another app.  Somewhat simpler would be to do this for a record structure.

HTH
Steve Belkin

Other Threads