[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2003-10-28
7
Medium Priority
?
758 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 180 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

656 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