?
Solved

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

Posted on 2003-10-28
7
Medium Priority
?
751 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.
Suggested Courses

752 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