BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Copy an object into a VARIANT???

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi ActiveX Controls Development
View previous topic :: View next topic  
Author Message
amith
Guest





PostPosted: Tue Nov 04, 2003 4:42 am    Post subject: Copy an object into a VARIANT??? Reply with quote



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





PostPosted: Tue Nov 04, 2003 11:27 am    Post subject: Re: Copy an object into a VARIANT??? Reply with quote



< I need to pass VARIANT which holds an object as one of the
parameter from COM client.
Quote:


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





PostPosted: Wed Nov 05, 2003 6:10 am    Post subject: Re: Copy an object into a VARIANT??? Reply with quote



Hai Deborah,

Many thanks for your suggestions Smile.

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





PostPosted: Wed Nov 05, 2003 12:29 pm    Post subject: Re: Copy an object into a VARIANT??? Reply with quote

< Do you mean to say that i have to copy my object into a
SafeArray and then copy the SafeArray into the variant?
Quote:


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





PostPosted: Thu Nov 06, 2003 10:47 am    Post subject: Re: Copy an object into a VARIANT??? Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi ActiveX Controls Development All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.