• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 523
  • Last Modified:

How do I pass an object using a variant?

I have an object
class demo
 COleVariant a,b,c;
 CString str;
 bool t[3];

I would like to pass this object from one active X Control to another, using a Variant. Could anyone tell me how to do this? A code example in Visual C++ would be very welcome.

Thanks in advance
1 Solution
As far as I know variant is a class that can contain a variety of different data types. The marshalling of the data is done internal to the class and conversions between some of the types are also supported.

In order to pass your object as a variant you will have to add the functionality to the variant object (which I am not sure VC will allow you to do?) or pass the data as a wchar array and do the marshalling yourself.

I am not that big on Visual C++ and try to avoid variants as a general rule so this might not be spot on but it is just my 2c

If you're passing the structure, you could pass it along in a variant that contains a SAFEARRAY, then "repackage" the array elements into your object on the receiving end.

Take a look at the "Corba tools" section from the bottom up:
1. Inprise/Borland have a compiler that works on PC's Win and Linux and Non PC Unix.
2. Rational gives you tools to do Analysis and Design and then put out an IDL file, with C++ code skeletons ready for compiling.
3. The free tools plug in to almost anything including the:
idl2cpp and idltocpp tools for unix.

Have fun. (It's easy)

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

woops answer for different question!
OK so I'll give you my answer for this one:

If it was a BSTR in a SayBSTR() method, you would:
MessageBox(NULL,  (char *)_bstr_t(theWords), “TheObject says”, MB_OK);

So if it's a VARIANT in a "SayVar()" method, you just
_variant_t() it instead of _bstr_t().

Same for any other usage.

Hope this helps.  Moshe
The advantage of variants (and the reason that it is most often not a good idea to pass structures as a safe array) is that marshalling between the client and server is done. This means that if the server is say a 64 bit big endian machine (such as a big Compaq server or SGI box) and the client is a Win32 Intel 32bit little endian machine the variables will be handled the same on both sides.

Another problem passing the data in an array occurs due to the setting of the byte-alignment of the compiler at compile time. If it is different for the client and server application the structure will come out bogus on the other side.

If you are passing floating point values such as floats or doubles you have the floating point representation to contend with as well.

If you use the variant types supplied you need not worry and it is all done for you and that is the reason why Variants are usually used for ActiveX and DCOM applications where the client and server is not garuanteed to have the same makeup.
I can Suggest One Solution.
U can Use CComVariant class which takes IUnknown* as a paramter for constructor.
CComVariant TempVar( (IUnknown*)object of class Demo);
Now pass this TempVar to the other object.
In other object accept it in a CComVariant .Then Use CComVariant class methods to retrieve IUnknown* & type cast it to your class i.e Demo.
Hope this works.
U can even try out _variant_t but first try CComVariant
yakkyAuthor Commented:
Thanks for everyones help

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now