onlyshiza
asked on
How to assign an object to a VARIANT in C++?
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
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
Am I way off? The above code is crashing btw.
Thanks in advance,
onlyshiza
Could you pass it as an IUnknown * rather than through a variant.
ASKER
How can I do this? that method just accepts VARIANT*
- thought you might be able to change the other method.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 );
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
ASKER
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.
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.
ASKER
VARIANT v;
v.vt = VT_UNKNOWN;
v.punkVal = pInterfaceOne;
pOtherInterface->AddVector