Solved

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

Posted on 2003-10-28
7
698 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
Comment Utility
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
Comment Utility
Could you pass it as an IUnknown * rather than through a variant.

0
 
LVL 2

Author Comment

by:onlyshiza
Comment Utility
How can I do this? that method just accepts VARIANT*
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 12

Expert Comment

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

Accepted Solution

by:
andrewjb earned 60 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now