Solved

How to assign an object to a VARIANT in C++?

Posted on 2003-10-28
7
743 Views
Last Modified: 2012-06-27
Hi everyone,

I am writing a client for a COM dll. In one of the function calls a pointer to an interface is returned (I am using smart pointers)

interfaceOnePtr pInterfacetOne = GiveMeTheVectors();

-Now the returned pointer is need to be passed into another function in the dll. The prototype of that function is:
AddVectors( VARIANT  *item);

How can I pass the returned pointer (pInterfaceOne) to the AddVectors method? How can I assign an object to a variant?
I was looking on the internet and didn't find anything straightforward. Is that true that I have to use the VT_DISPATCH ? Right now I am trying this:

VARIANT v;
v.vt = VT_DISPATCH; // what should I do here instead?
v.pdispVal = pInterfaceOne.Detach();
pOtherInterface->AddVector( &v );

Am I way off? The above code is crashing btw.
Thanks in advance,

onlyshiza      
0
Comment
Question by:onlyshiza
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 2

Author Comment

by:onlyshiza
ID: 9634604
ok, now I am trying this but still crashes:

VARIANT v;
v.vt = VT_UNKNOWN;
v.punkVal = pInterfaceOne;
pOtherInterface->AddVector( &v );
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 9635170
Could you pass it as an IUnknown * rather than through a variant.

0
 
LVL 2

Author Comment

by:onlyshiza
ID: 9635187
How can I do this? that method just accepts VARIANT*
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:andrewjb
ID: 9635210
- thought you might be able to change the other method.
0
 
LVL 12

Accepted Solution

by:
andrewjb earned 60 total points
ID: 9635225
Your second one looks OK, but you'd better make sure the interface is addref-d
so, either

v.punkVal - pInterfaceOne.Detach();

or

v.punkVal->AddRef() after assigning.

I'd typically use CComVariant as a helper:

CComVariant v(pInterfaceOne);

pOther->AddVec( &v );

does everytyhing for you.

0
 

Expert Comment

by:jbuote1
ID: 9636242
Hi,
I'm still new but if I'm not mistaken, you need to initialize the variant to empty first.
VariantInit() does this.


VARIANT v;
VariantInit(&v);
v.vt = VT_DISPATCH; // what should I do here instead?
v.pdispVal = pInterfaceOne.Detach();
pOtherInterface->AddVector( &v );


0
 
LVL 2

Author Comment

by:onlyshiza
ID: 9661083
thanks for your replies and sorry for delay.
I have tried your suggestions and still the program crashes. Maybe the problem is from another place. I'll assign the points to andrewjb though for his kind effort.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question