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