Board index » delphi » Marshalling a record structure ?

Marshalling a record structure ?

I am wanting to pass a record structure via an OleVariant. I am using
Midas, which uses an OleVariant to communicate between server and
client.

The Delphi help says that "Any custom record type defined within the
type library" is an Automation compatible type.

I have created my record structure using the type editor.

However I don't know how to tell the compiler to assign my variable (of
the record type as per the the type library) to the OleVariant. I get a
type mismatch or a invalid typecast.

How do I code the assignment, or is there a restriction that I have
overlooked.

If it is not possible to marshall a record structure via Midas, what are
my alternatives ? I have multiple pieces of information that I want pass
from the client to the server at the one time.

thanks
Barry Mossman

 

Re:Marshalling a record structure ?


Quote
"Barry Mossman" <BarryMoss...@csi.com> wrote in message

news:3b4a83c1_2@dnews...

Quote
> If it is not possible to marshall a record structure via Midas, what
are
> my alternatives ? I have multiple pieces of information that I want
pass
> from the client to the server at the one time.

I see that I can pass across a variant array, but I am hoping to find
something a little more self documenting than va[0], va[1] regarding
"field names".

thanks
Barry Mossman

Re:Marshalling a record structure ?


I was going to suggest a VarArray.  And don't forget you can pack a VarArray
if it contains standard datatypes-this speeds up the handling of larger
arrays.

Regarding the fieldnames issue we create an enumerated type so that we can
address the Variant array elements with sensible values.  Make sure that the
enumerated type is used to pack to the array on the server and to unpack
again on the client to keep consistancy.  Also don't forget that the
enumerated type members must be cast as an integer when using them to access
the variant array elements.

[vaFORENAME, vaSURNAME, etc]

Variable := VarArray[Integer(vaFORENAME)];

and you can also define the variant array using the high value of the
enumerated type to ensure that it contains the correct number of elements.
That's the best method we found of handling vararrays, if you think of
anything better let me know.

Martin.

Quote
"Barry Mossman" <BarryMoss...@csi.com> wrote in message

news:3b4a96a6$1_2@dnews...
Quote
> "Barry Mossman" <BarryMoss...@csi.com> wrote in message
> news:3b4a83c1_2@dnews...
> > If it is not possible to marshall a record structure via Midas, what
> are
> > my alternatives ? I have multiple pieces of information that I want
> pass
> > from the client to the server at the one time.

> I see that I can pass across a variant array, but I am hoping to find
> something a little more self documenting than va[0], va[1] regarding
> "field names".

> thanks
> Barry Mossman

Re:Marshalling a record structure ?


This is a limitation of Delphi OleVariants. There are actually COM APIs you
can use to embed structs into VARIANTs.

I can see 2 alternatives:

1) a VARIANT array: VarArrayCreate (), ...

2) XML string

An XML string should be self-describing and probably more of what you want.

--
have fun
Binh Ly
http://www.techvanguards.com

Quote
"Barry Mossman" <BarryMoss...@csi.com> wrote in message

news:3b4a83c1_2@dnews...
Quote
> I am wanting to pass a record structure via an OleVariant. I am using
> Midas, which uses an OleVariant to communicate between server and
> client.

> The Delphi help says that "Any custom record type defined within the
> type library" is an Automation compatible type.

> I have created my record structure using the type editor.

> However I don't know how to tell the compiler to assign my variable (of
> the record type as per the the type library) to the OleVariant. I get a
> type mismatch or a invalid typecast.

> How do I code the assignment, or is there a restriction that I have
> overlooked.

> If it is not possible to marshall a record structure via Midas, what are
> my alternatives ? I have multiple pieces of information that I want pass
> from the client to the server at the one time.

> thanks
> Barry Mossman

Re:Marshalling a record structure ?


Quote
> <SNIP>Regarding the fieldnames issue we create an enumerated type so that
we can
> address the Variant array elements with sensible values. ...<SNIP>

We also do this, and our variant arrays are wrapped up in classes which
allow accessing the individual elements as properties e.g

TSomeArray = class(TVariantArrayMember)
  FData:Olevariant; {Stores the variant array}
  ..
public
  property SomeField:string read GetSomeField write SetSomeField;
end;
..
implementation

function TSomeArray.GetSomeField:string;
begin
  Result := FData[saSomeField]; {Use a enumerated type for more readability}
end;

procedure TSomeArray.SetSomeField(Value:string);
begin
  FData[saSomeField] := Value; {Use a enumerated type for more readability}
end;

Obviously, this is a real cut down version, but it should give some idea.
--
Cheers,
David Clegg
dcl...@ebetonline.com

Re:Marshalling a record structure ?


Quote
"David Clegg" <dcl...@ebetonline.com> wrote in message

news:3b4bc513_1@dnews...> Obviously, this is a real cut down version,
but it should give some idea.

thanks David, Martin & Binh. I can see what I need to do now.

Barry Mossman

Re:Marshalling a record structure ?


Quote
"David Clegg" <dcl...@ebetonline.com> wrote in message

news:3b4bc513_1@dnews...

Quote
> We also do this, and our variant arrays are wrapped up in classes
which
> allow accessing the individual elements as properties e.g

> TSomeArray = class(TVariantArrayMember)

David, what is TVariantArrayMember ? I am assuming that I am making a
class that I can assign to an OleVariant, but I can't find
TvariantArrayMember in my Delphi source to descend from. I am fairly new
to class creation, so I don't know what class to use here.

thanks

Barry Mossman

Re:Marshalling a record structure ?


Sorry, I cut and pasted an example from our source. TVariantArrayMember is
base class we created which has common methods used by our variant array
classes.  You could simply do

TSomeArray = class
..
private
..
public
..
end;

--
Cheers,
David Clegg
dcl...@ebetonline.com

Quote
"Barry Mossman" <BarryMoss...@csi.com> wrote in message

news:3b4bf2b4_1@dnews...
Quote
> "David Clegg" <dcl...@ebetonline.com> wrote in message
> news:3b4bc513_1@dnews...
> > We also do this, and our variant arrays are wrapped up in classes
> which
> > allow accessing the individual elements as properties e.g

> > TSomeArray = class(TVariantArrayMember)

> David, what is TVariantArrayMember ? I am assuming that I am making a
> class that I can assign to an OleVariant, but I can't find
> TvariantArrayMember in my Delphi source to descend from. I am fairly new
> to class creation, so I don't know what class to use here.

> thanks

> Barry Mossman

Re:Marshalling a record structure ?


Quote
"David Clegg" <dcl...@ebetonline.com> wrote in message

news:3b4cd72b_2@dnews...
Quote
> Sorry, I cut and pasted an example from our source.

TVariantArrayMember is

Quote
> base class we created which has common methods used by our variant
array
> classes.  You could simply do

Thanks David, that got it going for me.

It looks like it could be exciting for you if Borland publishes a
TVariantArrayMember type in Delphi 7+. <g>

Barry Mossman

Re:Marshalling a record structure ?


Quote
> It looks like it could be exciting for you if Borland publishes a
> TVariantArrayMember type in Delphi 7+. <g>

> Barry Mossman

Won't be a problem. After the time it took us to finally convert all our
exes and dlls from D2 to D5 (mainly due to Quickreports), I can't see
management authorising an upgrade for a LONG time <g>.

P.S. If you're new to COM/DCOM, I suggest checking out Binh Ly's site at
http://www.techvanguards.com . Heaps of tips and examples.
--
Cheers,
David Clegg
dcl...@ebetonline.com

Re:Marshalling a record structure ?


Quote
"David Clegg" <dcl...@ebetonline.com> wrote in message

news:3b4d4715$1_2@dnews...

Quote
> Won't be a problem. After the time it took us to finally convert all
our
> exes and dlls from D2 to D5 (mainly due to Quickreports), I can't see
> management authorising an upgrade for a LONG time <g>.

Oh, ... bad luck for Borland, and for you. ... At least you didn't jump
back into the swim at D4 <g>

Thanks for url to Binh's excellent site.

Barry Mossman

Other Threads