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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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)

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yakkyAuthor Commented:
Thanks for everyones help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.