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
Solved

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

Posted on 2003-10-28
7
728 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
  • 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 141
GUI: DIalog Stacking and Popping in MS C++ 4 80
Eclipse IDE - Cannot copy/paste from console output 8 251
Outlook 13 41
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

808 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