 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
amith Guest
|
Posted: Tue Nov 04, 2003 4:42 am Post subject: Copy an object into a VARIANT??? |
|
|
Hai all,
I need to pass VARIANT which holds an object as one of the parameter from
COM client. I need to reconstruct back the object from the VARIANT in the
server. How do I copy a user defined object into a VARIANT or OleVariant?
and reconstruct back at the server end?
Please advice.
--
Amith.
|
|
| Back to top |
|
 |
Deborah Pate (TeamB) Guest
|
Posted: Tue Nov 04, 2003 11:27 am Post subject: Re: Copy an object into a VARIANT??? |
|
|
<
I need to pass VARIANT which holds an object as one of the
parameter from COM client.
The COMish thing to do is to use interfaces - make your
object implement an interface and pass that.
You can also pass your object's data as a variant array of
bytes, and reconstitute it at the other end. If you do
that, though, you obviously need to make sure that you
don't accidentally pass any pointers instead.
--
Deborah Pate (TeamB) http://delphi-jedi.org
TeamB don't see posts sent via Google or ISPs
Use the real Borland server: newsgroups.borland.com
http://www.borland.com/newsgroups/genl_faqs.html
|
|
| Back to top |
|
 |
amith Guest
|
Posted: Wed Nov 05, 2003 6:10 am Post subject: Re: Copy an object into a VARIANT??? |
|
|
Hai Deborah,
Many thanks for your suggestions .
Regarding your second suggestion:
<
You can also pass your object's data as a variant array of
bytes >>
Do you mean to say that i have to copy my object into a SafeArray and then
copy the SafeArray into the variant?
One more question is: My COM method accepts OleVariant as a parameter. But I
am using TVariantArg and TSafeArrayBound for putting the object in to the
safearray and then to variant. OleVariant and TVariantArg are incompatible.
How do i do the conversion?
sample snippet:
VntSal: TVariantArg;
SalObj: TgtSalClass;
bound: TSafeArrayBound;
nBytes: Integer;
pDest: pointer;
nBytes := SizeOf(SalObj);
bound.lLbound := 0;
bound.cElements := nBytes;
VntSal.vt := VT_UI1 or VT_ARRAY;
VntSal.parray := SafeArrayCreate(VT_UI1, 1, bound);
SafeArrayAccessData(VntSal.parray, pDest);
CopyMemory(pDest, addr(SalObj), nBytes);
SafeArrayUnaccessData(VntSal.parray);
gtMyComponentXX1.SetSal(VntSal); --> my COM method accepts OleVariant, i
cannot pass TVariantArg
Please give your thoughts.
--
Amith.
"Deborah Pate (TeamB)" <d.pate (AT) blueyonder (DOT) co.not-this-bit.uk> wrote in
message news:VA.00001d9f.0a6b5102 (AT) blueyonder (DOT) co.not-this-bit.uk...
| Quote: | Amith:
I need to pass VARIANT which holds an object as one of the
parameter from COM client.
The COMish thing to do is to use interfaces - make your
object implement an interface and pass that.
You can also pass your object's data as a variant array of
bytes, and reconstitute it at the other end. If you do
that, though, you obviously need to make sure that you
don't accidentally pass any pointers instead.
--
Deborah Pate (TeamB) http://delphi-jedi.org
TeamB don't see posts sent via Google or ISPs
Use the real Borland server: newsgroups.borland.com
http://www.borland.com/newsgroups/genl_faqs.html
|
|
|
| Back to top |
|
 |
Deborah Pate (TeamB) Guest
|
Posted: Wed Nov 05, 2003 12:29 pm Post subject: Re: Copy an object into a VARIANT??? |
|
|
<
Do you mean to say that i have to copy my object into a
SafeArray and then copy the SafeArray into the variant?
No, you don't need to use SafeArray functions, you can use
Delphi's variant routines. Here are a couple of functions
showing how to put a memorystream into and out of a
variant, as a variant array of bytes:
function MemoryStreamToOleVariant(const Strm:
TMemoryStream): OleVariant;
var
Data: PByteArray;
begin
Result := VarArrayCreate ([0, Strm.Size - 1], varByte);
Data := VarArrayLock(Result);
try
Strm.Position := 0;
Strm.ReadBuffer(Data^, Strm.Size);
finally
VarArrayUnlock(Result);
end;
end;
function OleVariantToMemoryStream(const OV: OleVariant):
TMemoryStream;
var
Data: PByteArray;
Size: integer;
begin
Result := TMemoryStream.Create;
try
Size := VarArrayHighBound (OV, 1) - VarArrayLowBound
(OV, 1) + 1;
Data := VarArrayLock(OV);
try
Result.Position := 0;
Result.WriteBuffer(Data^, Size);
finally
VarArrayUnlock(OV);
end;
except
Result.Free;
Result := nil;
end;
end;
--
Deborah Pate (TeamB) http://delphi-jedi.org
TeamB don't see posts sent via Google or ISPs
Use the real Borland server: newsgroups.borland.com
http://www.borland.com/newsgroups/genl_faqs.html
|
|
| Back to top |
|
 |
amith Guest
|
Posted: Thu Nov 06, 2003 10:47 am Post subject: Re: Copy an object into a VARIANT??? |
|
|
Hai Deborah,
Thanks a lot for your suggestion.
--
Amith.
"Deborah Pate (TeamB)" <d.pate (AT) blueyonder (DOT) co.not-this-bit.uk> wrote in
message news:VA.00001da4.0049bdce (AT) blueyonder (DOT) co.not-this-bit.uk...
| Quote: | Amith:
Do you mean to say that i have to copy my object into a
SafeArray and then copy the SafeArray into the variant?
No, you don't need to use SafeArray functions, you can use
Delphi's variant routines. Here are a couple of functions
showing how to put a memorystream into and out of a
variant, as a variant array of bytes:
function MemoryStreamToOleVariant(const Strm:
TMemoryStream): OleVariant;
var
Data: PByteArray;
begin
Result := VarArrayCreate ([0, Strm.Size - 1], varByte);
Data := VarArrayLock(Result);
try
Strm.Position := 0;
Strm.ReadBuffer(Data^, Strm.Size);
finally
VarArrayUnlock(Result);
end;
end;
function OleVariantToMemoryStream(const OV: OleVariant):
TMemoryStream;
var
Data: PByteArray;
Size: integer;
begin
Result := TMemoryStream.Create;
try
Size := VarArrayHighBound (OV, 1) - VarArrayLowBound
(OV, 1) + 1;
Data := VarArrayLock(OV);
try
Result.Position := 0;
Result.WriteBuffer(Data^, Size);
finally
VarArrayUnlock(OV);
end;
except
Result.Free;
Result := nil;
end;
end;
--
Deborah Pate (TeamB) http://delphi-jedi.org
TeamB don't see posts sent via Google or ISPs
Use the real Borland server: newsgroups.borland.com
http://www.borland.com/newsgroups/genl_faqs.html
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|